1.自己发送的广播,只有指定的app才能接收(通过权限设置)
//sender app
1.在Androidmanifest.xml中声明新的权限:
<permission
android:name="com.self.permission.recv_broadcast"
android:protectionLevel="normal" />
2.发送广播时,指定权限:
sendBroadcast("com.xxx.xxx.action","com.self.permission.recv_broadcast");
经过以上2步,使得只有具备com.self.permission.recv_broadcast权限的Receiver才能接收该广播。
故,要接收该广播,接收的app应该在其Androidmanifest.xml中声明使用该权限:
<uses-permission android:name="com.self.permission.recv_broadcast" />
然后,静态注册则还需在AndroidMainfest.xml中声明:
<receiver android:name=".XXXReceiver"
android:permission="com.self.permission.recv_broadcast">
<intent-filter>
<action android:name="com.android.XXX_ACTION" />
</intent-filter>
</receiver>
或者:
//动态注册
registerReceiver(receiver, "com.self.permission.recv_broadcast",null);
经测试,只要在接收特定权限广播的AndroidMainfest.xml中声明:
<uses-permission android:name="xxx.permission.xx" />
,那么即使不使用registerReceiver(receiver, "xxx.permission.xx",null)权限注册方式,而采用简单的registerReceiver(receiver,filter),这样,也是可以接收到特定权限广播的,因为xml里面已经声明了权限(注:该种方式只对普通应用有效,系统签名的应用通过这种不指明权限的注册方法,是不能接收到相应广播的)。
2.自己接收广播,只有指定的广播才会接收(也是通过权限设置)
//receiver app
1.因为是自己接收特定广播,故该广播也是需要某一特定权限,故先在Androidmanifest.xml中声明一个新的权限:
<permission android:name="com.self.permission.send_broadcast" />
2.然后,在自己(Receiver)的<receiver>tag中添加权限声明:
<receiver android:name=".XXXReceiver"
android:permission="com.self.permission.send_broadcast">
<intent-filter>
<action android:name="com.android.XXX_ACTION" />
</intent-filter>
</receiver>
这样一来,该receiver便只能接收来自具有"com.self.permission.send_broadcast"权限的应用发出的广播。
3.要发送这种广播,只需在Sender App的AndroidManifest.xml中也声明使用该权限即可:
<uses-permission android:name="com.self.permission.send_broadcast" />
转载于:https://blog.51cto.com/whatever957/1832098