首先我们来理解下监听器的机制。
android的事件处理机制有两种:监听和回调。
a基于监听的事件处理
主要涉及三类对象:eventsource(事件源),event(事件),eventlistener(事件监听器)
监听机制处理事件的流程图如下(委派式:delegation):
1:需要在androidmanifest.xml清单中添加权限
1
2
|
<uses-permission android:name= "android.permission.process_outgoing_calls" /> <uses-permission android:name= "android.permission.read_phone_state" /> |
2:注册广播 要注意事件的级别 (android中的级别-1000~1000)
1
2
3
4
5
6
|
<receiver android:name= ".phonebroadcastreceiver" > <intent-filter android:priority= "1000" > <action android:name= "android.intent.action.new_outgoing_call" /> <action android:name= "android.intent.action.phone_state" /> </intent-filter> </receiver> |
详细配置请看 androidmanifest.xml
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
|
<manifest xmlns:android= "http://schemas.android.com/apk/res/android" package = "com.zyw.broadcastsendsms" android:versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "15" /> <application android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= ".sendsms" android:label= "@string/title_activity_send_sms" > <intent-filter> <action android:name= "android.intent.action.main" /> <category android:name= "android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name= ".phonebroadcastreceiver" > <intent-filter android:priority= "1000" > <action android:name= "android.intent.action.new_outgoing_call" /> <action android:name= "android.intent.action.phone_state" /> </intent-filter> </receiver> </application> <uses-permission android:name= "android.permission.process_outgoing_calls" /> <uses-permission android:name= "android.permission.read_phone_state" /> </manifest> |
3:编写广播的实现类 这里需要继承broadcastreceiver 实现onreceive()方法
程序的源代码如下:
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
|
package com.zyw.broadcastsendsms; import android.app.service; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.telephony.telephonymanager; import android.util.log; public class phonebroadcastreceiver extends broadcastreceiver { private static final string tag = "message" ; private static boolean mincomingflag = false ; private static string mincomingnumber = null ; @override public void onreceive(context context, intent intent) { // 如果是拨打电话 if (intent.getaction().equals(intent.action_new_outgoing_call)) { mincomingflag = false ; string phonenumber = intent.getstringextra(intent.extra_phone_number); log.i(tag, "call out:" + phonenumber); } else { // 如果是来电 telephonymanager tmanager = (telephonymanager) context .getsystemservice(service.telephony_service); switch (tmanager.getcallstate()) { case telephonymanager.call_state_ringing: mincomingnumber = intent.getstringextra( "incoming_number" ); log.i(tag, "ringing :" + mincomingnumber); break ; case telephonymanager.call_state_offhook: if (mincomingflag) { log.i(tag, "incoming accept :" + mincomingnumber); } break ; case telephonymanager.call_state_idle: if (mincomingflag) { log.i(tag, "incoming idle" ); } break ; } } } /*@override public void onreceive(context context, intent intent) { string number = getresultdata(); if("5556".equals(number)){ setresultdata(null);//挂断 }else{ number = "12593"+ number; //其他,则加区号 setresultdata(number); } }*/ } |
以上内容给大家介绍了android广播接收实现监听电话状态(电话的状态,拦截)的相关知识,希望对大家有所帮助!