利用服务实现电话监听(2)

清单设置(一个receiver,一个service,若干权限)

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<uses-permission android:name="android.permission.RECORD_AUDIO"/>

<uses-permission android:name=

                     "android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<uses-permission android:name=

                     "android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.RECEIVE_SMS"/>

<application …>

        <receiver android:name="BootBroadcastReceiver">

            <intent-filter>

                <action android:name=

                           "android.provider.Telephony.SMS_RECEIVED"/>

            </intent-filter>

        </receiver>

<service android:name="PhoneListenerService" />

 

电话监听器服务类:PhoneListenerService

package cn.class3g.phonelistener;

...

public class PhoneListenerService extends Service {

 

    public IBinder onBind(Intent arg0) {

       return null;

    }

 

    public void onCreate() {

       super.onCreate();

       Log.i("TAG", "服务启动了");

 

       // 对电话的来电状态进行监听

       TelephonyManager telManager = (TelephonyManager) this

              .getSystemService(Context.TELEPHONY_SERVICE);

       // 注册一个监听器对电话状态进行监听

       telManager.listen(new MyPhoneStateListener(),

              PhoneStateListener.LISTEN_CALL_STATE);

    }

 

    private class MyPhoneStateListener extends PhoneStateListener {

       MediaRecorder recorder;

       File audioFile;

       String phoneNumber;

 

       public void onCallStateChanged(int state, String incomingNumber) {

           switch (state) {

           case TelephonyManager.CALL_STATE_IDLE: /* 无任何状态时 */

              if (recorder != null) {

                  recorder.stop();// 停止刻录

                  recorder.reset();// 重设

                  recorder.release();// 刻录完成一定要释放资源

              }

              break;

           case TelephonyManager.CALL_STATE_OFFHOOK: /* 接起电话时 */

 

              try {

                  recorder = new MediaRecorder();

                  recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 设置音频采集原

                  recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// 内容输出格式

                  recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // 音频编码方式

 

                  // recorder.setOutputFile("/sdcard/myvoice.amr");

                  audioFile = new File(

                         Environment.getExternalStorageDirectory(),

                         phoneNumber + "_" + System.currentTimeMillis()

                                + ".3gp");

                  recorder.setOutputFile(audioFile.getAbsolutePath());

                  Log.i("TAG", audioFile.getAbsolutePath());

 

                  recorder.prepare(); // 预期准备

                  recorder.start();

 

              } catch (IllegalStateException e) {

                  e.printStackTrace();

              } catch (IOException e) {

                  e.printStackTrace();

              }

 

              break;

           case TelephonyManager.CALL_STATE_RINGING: /* 电话进来时 */

              phoneNumber = incomingNumber;

               break;

           default:

              break;

           }

           super.onCallStateChanged(state, incomingNumber);

       }

    }

}

 

利用开机启动广播启动服务(实验环境中为了方便起见可以先改用短消息广播)

开机启动完成广播action

<action android:name="android.intent.action.BOOT_COMPLETED"/>

package cn.class3g.phonelistener;

...

public class BootBroadcastReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

Log.i("TAG", "广播被接收了");

Intent serviceIntent = new Intent(context, PhoneListenerService.class);

context.startService(serviceIntent);

}

}

 

测试:

1.        启动模拟器,部署应用

2.        利用模拟器控制器发送短信启动服务(查看日志输出判断是否成功)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyzjff.html