使用发送有序广播方法,广播优先级才能生效。
1
2
3
4
5
6
7
8
9
10
11
12
|
findViewById(R.id.button1).setOnClickListener(
new
OnClickListener()
{
@Override
public
void
onClick(View v)
{
Intent intent =
new
Intent();
intent.setAction(
"com.example.aex56"
);
intent.putExtra(
"key"
,
"value"
);
sendOrderedBroadcast(intent,
null
);
//发送有序广播
}
});
|
XML文件设置广播优先级:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<receiver
android:name=
"com.example.aex56_ordered_broadcast.MyReceiver1"
android:enabled=
"true"
android:exported=
"true"
>
<intent-filter android:priority=
"2500"
>
<action android:name=
"com.example.aex56"
/>
</intent-filter>
</receiver>
<receiver
android:name=
"com.example.aex56_ordered_broadcast.MyReceiver2"
android:enabled=
"true"
android:exported=
"true"
>
<intent-filter android:priority=
"3000"
>
<action android:name=
"com.example.aex56"
/>
</intent-filter>
</receiver>
|
接收电话呼出广播,自动添加号码前缀呼出;设置短信接收者最高优先级,拦截短信。
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
30
31
32
33
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<manifest xmlns:android=
"http://schemas.android.com/apk/res/android"
package
=
"com.example.aex58_ordered_tel"
android:versionCode=
"1"
android:versionName=
"1.0"
>
<uses-sdk
android:minSdkVersion=
"8"
android:targetSdkVersion=
"16"
/>
<uses-permission android:name=
"android.permission.PROCESS_OUTGOING_CALLS"
/>
<uses-permission android:name=
"android.permission.RECEIVE_SMS"
/>
<application
android:allowBackup=
"true"
android:icon=
"@drawable/ic_launcher"
android:label=
"@string/app_name"
android:theme=
"@style/AppTheme"
>
<receiver
android:name=
"com.example.aex58_ordered_tel.MyTelReceiver"
android:enabled=
"true"
android:exported=
"true"
>
<intent-filter>
<action android:name=
"android.intent.action.NEW_OUTGOING_CALL"
/>
</intent-filter>
</receiver>
<receiver
android:name=
"com.example.aex58_ordered_tel.MySmsReceiver"
android:enabled=
"true"
android:exported=
"true"
>
<intent-filter android:priority=
"9999999999999999999999999999999"
>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED"
/>
</intent-filter>
</receiver>
</application>
</manifest>
|
电话呼出广播接收:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public
class
MyTelReceiver
extends
BroadcastReceiver
{
//监测电话号码
/*1.添加电话呼出权限
* uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"
*
*2.接收系统电话呼出的广播
* <intent-filter >
* <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
* </intent-filter>
*
* 其他系统广播:
* android.intent.action.BOOT_COMPLETED 开机启动广播
*/
@Override
public
void
onReceive(Context context, Intent intent)
{
String telnum = getResultData();
//得到电话号码
telnum =
"0592"
+ telnum;
setResultData(telnum);
//设置拨出的电话号码
}
}
|
短信接收者:
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
|
public
class
MySmsReceiver
extends
BroadcastReceiver
{
/*
* 拦截短信
* 1.添加短信接收权限
* uses-permission android:name="android.permission.RECEIVE_SMS"
*
* 2.设置广播优先级,接收系统收短信的广播
* <intent-filter android:priority="99999999999999999999999999"> 设置广播优先级
* <action android:name="android.provider.Telephony.SMS_RECEIVED"/>接收系统收到短信时的广播
* </intent-filter>
*
* 3.重写onReceive
* abortBroadcast();
*/
@Override
public
void
onReceive(Context context, Intent intent)
{
Log.e(
"msg"
,
"mmm"
);
abortBroadcast();
//关闭广播
}
}
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1211541,如需转载请自行联系原作者