• Service之来电监听(失败的案例)


    Service:服务,可以理解成一个运行再后台没有界面的Activity,集成于Seriver,是四大组件之一

    Service的继承关系:Service-->ContextWrapper-->Context  (与activity一样,都继承与ContextWrapper);

    生命周期:Context.startSeriver() 方法开启服务:onCreate()-->onStartCommand()-->onDestory();

        第一次开启服务,服务会执行onCreate()-->onStartCommand();(开启之后再开启服务,不会执行onCreate()方法);

         Context.bindSeriver() 方法开启服务:onCreate()-->onBind()-->onUnbind()-->重载的方法返回true-->Rebind()-->onDestory();

    开启

       方式一:startService();

       方式二:bindService();

    停止:

      方式一:stopService();

      方式二:unBind();

    下面开始我们的来电监听:

    实现创建一个类继承与Seriver:

     1 public class MyService extends Service {
     2     //创建一个录音机的实例
     3     MediaRecorder recorder = null;
     4     //绑定服务
     5     @Nullable
     6     @Override
     7     public IBinder onBind(Intent intent) {
     8         return null;
     9     }
    10 
    11     //解绑服务
    12     @Override
    13     public void unbindService(ServiceConnection conn) {
    14         super.unbindService(conn);
    15     }
    16 
    17     //重新绑定服务
    18     @Override
    19     public void onRebind(Intent intent) {
    20         super.onRebind(intent);
    21     }
    22 
    23     //创建一个Service 第一次开启的时候
    24     @Override
    25     public void onCreate() {
    26         Log.i("wei","onCreate");
    27         TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
    28         tm.listen(new MyPhoneStateSeriverListener(),PhoneStateListener.LISTEN_CALL_STATE);
    29         super.onCreate();
    30 
    31     }
    32 
    33     @Override
    34     public int onStartCommand(Intent intent, int flags, int startId) {
    35         Log.i("wei","onStartCommand");
    36         return super.onStartCommand(intent, flags, startId);
    37     }
    38 
    39     @Override
    40     public void onDestroy() {
    41         super.onDestroy();
    42         Log.i("wei","onDestroy");
    43     }
    44     private class MyPhoneStateSeriverListener extends PhoneStateListener{
    45         @Override
    46         public void onCallStateChanged(int state, String incomingNumber) {
    47             switch (state){
    48                 case TelephonyManager.CALL_STATE_IDLE://空闲状态
    49                     if (recorder!=null){
    50                         recorder.stop();
    51                         recorder.reset();
    52                         recorder.release();
    53                     }
    54                     break;
    55                 case TelephonyManager.CALL_STATE_OFFHOOK://接听状态
    56                     recorder.start();//开始录音
    57                     break;
    58                 case TelephonyManager.CALL_STATE_RINGING://响铃状态
    59                     recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);//设置音频的来源  MIC只能录单方的,VOICE_CALL可以录双方的
    60                     recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//设置音频的输出格式
    61                     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//设置音频的编码方式
    62                     recorder.setOutputFile("/mnt/sdcard/luyin.3gp");//设置录音文件的存储位置
    63                     try {
    64                         recorder.prepare();//准备录音
    65                     } catch (IOException e) {
    66                         e.printStackTrace();
    67                     }
    68                     break;
    69             }
    70             super.onCallStateChanged(state, incomingNumber);
    71         }
    72     }
    73 }

    Manifest.xml配置:

    1 <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    2     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    3     <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    报错,空指针异常。

       

    GitHub:https://github.com/godfunc
    博客园:http://www.cnblogs.com/godfunc
    Copyright ©2019 Godfunc
  • 相关阅读:
    Anagram
    HDU 1205 吃糖果(鸽巢原理)
    Codeforces 1243D 0-1 MST(补图的连通图数量)
    Codeforces 1243C Tile Painting(素数)
    Codeforces 1243B2 Character Swap (Hard Version)
    Codeforces 1243B1 Character Swap (Easy Version)
    Codeforces 1243A Maximum Square
    Codeforces 1272E Nearest Opposite Parity(BFS)
    Codeforces 1272D Remove One Element
    Codeforces 1272C Yet Another Broken Keyboard
  • 原文地址:https://www.cnblogs.com/Godfunc/p/6036743.html
Copyright © 2020-2023  润新知