本文实例讲述了Android编程之利用服务实现电话监听的方法。分享给大家供大家参考,具体如下:
1. 启动模拟器,部署应用
2. 利用模拟器控制器发送短信启动服务(查看日志输出判断是否成功)
3. 向模拟器拨打电话,并接听,挂断电话后,利用文件管理查看对应的cache目录或者sdcard中生成了3gp文件,并将其复制到pc中播放以验证。
清单设置(一个receiver,一个service,若干权限)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
< 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
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"/>
1
2
3
4
5
6
7
8
9
|
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); } } |
希望本文所述对大家Android程序设计有所帮助。