自定义广播权限例子:
发送者:Demo_BroadcastSend 发出广播(1、发出无权限广播按钮 , 2、发出带权限广播按钮)
接收者:Demo_BroadcastReceive_A 无自定义广播权限
接收者:Demo_BroadcastReceive_B 有自定义广播权限(manifest中)
<uses-permission android:name="com.sdf.userdefined.receiver.permission" />
<permission
android:name="com.sdf.userdefined.receiver.permission"
android:protectionLevel="normal" />
--------------------------------------------------------------------------------
Demo_BroadcastSend发出无权限广播,Demo_BroadcastReceive_A与Demo_BroadcastReceive_B中的广播接收器均可接收到改广播
结果:
[MyReceiver--->B] onReceive : intent = Intent { act=com.sdf.userdefined.receiver.action cat=[receiver] flg=0x10 cmp=com.sdf.demo_broadcastreceive_b/.MyReceiverB (has extras) }
[MyReceiver--->B] onReceive : message = 无 ---> 权限的广播
[MyReceiver--->A] onReceive : intent = Intent { act=com.sdf.userdefined.receiver.action cat=[receiver] flg=0x10 cmp=com.sdf.demo_broadcastreceive_a/.MyReceiverA (has extras) }
[MyReceiver--->A] onReceive : message = 无 ---> 权限的广播
--------------------------------------------------------------------------------
步骤1:
Demo_BroadcastSend 中(manifest)定义以下权限:
<uses-permission android:name="com.sdf.userdefined.send.permission" />
<permission
android:name="com.sdf.userdefined.send.permission"
android:protectionLevel="normal" />
步骤2:
Demo_BroadcastReceive_B 中(manifest)定义以下权限:
<uses-permission android:name="com.sdf.userdefined.receiver.permission" />
<permission
android:name="com.sdf.userdefined.receiver.permission"
android:protectionLevel="normal" />
且
Demo_BroadcastReceive_B 中 ( manifest)的广播接收器加上权限属性:
android:permission="com.sdf.userdefined.send.permission"
以上两步骤同时实现才可接收带权限的广播
结果:
[MyReceiver--->B] onReceive : intent = Intent { act=com.sdf.userdefined.receiver.action cat=[receiver] flg=0x10 cmp=com.sdf.demo_broadcastreceive_b/.MyReceiverB (has extras) }
[MyReceiver--->B] onReceive : message = 带 ---> 权限的广播
--------------------------------------------------------------------------------
广播发送者:Demo_BroadcastSend
Manifest.xml
<!-- 自定义权限 -->
<uses-permission android:name="com.sdf.userdefined.send.permission" />
<!-- 在声明权限时需要一个android:protectionLevel的属性,它代表“风险级别”:normal、dangerous、signature、signatureOrSystem -->
<permission
android:name="com.sdf.userdefined.send.permission"
android:protectionLevel="normal" />
Code :
发出无权限广播:
/**
* Demo_BroadcastReceive_A 与 Demo_BroadcastReceive_B
* 中均过滤接收该广播,因为intent的action一样
*/
Intent i = new Intent("com.sdf.userdefined.receiver.action");
i.addCategory("receiver");
i.putExtra("message", "无 ---> 权限的广播");
/**
* 不带权限,即只要有过滤com.sdf.userdefined.receiver.action即可接收该广播,即
* Demo_BroadcastReceive_A 与 Demo_BroadcastReceive_B 均可收到
*/
sendOrderedBroadcast(i, null);
发出带权限广播:
/**
* Demo_BroadcastReceive_A 与 Demo_BroadcastReceive_B
* 中均过滤接收该广播,因为intent的action一样
*/
Intent ii = new Intent("com.sdf.userdefined.receiver.action");
ii.addCategory("receiver");
ii.putExtra("message", "带 ---> 权限的广播");
/**
* com.sdf.userdefined.receiver.permission :
* 该permission在接收程序中的manifest中定义,即这边带权限的广播发出去,谁的应用中定义了该权限,谁才能接收到,
* 即只有Demo_BroadcastReceive_B 可收到
*/
sendOrderedBroadcast(ii, "com.sdf.userdefined.receiver.permission");
接收者A:Demo_BroadcastReceive_A
Manifest.xml
<!-- 带权限广播接收器 ,无自定义权限时,android:permission该属性可以去掉 -->
<receiver
android:name="com.sdf.demo_broadcastreceive_a.MyReceiverA"
android:permission="com.sdf.userdefined.send.permission" >
<intent-filter>
<action android:name="com.sdf.userdefined.receiver.action" /> <category android:name="receiver" />
</intent-filter></receiver>
Code:
package com.sdf.demo_broadcastreceive_a;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;/**
* 广播接收器
*
* @author dengfu.su
*
*/
public class MyReceiverA extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
Log.d("sdf", "[MyReceiver--->A] onReceive : intent = " + intent.toString());
Log.d("sdf", "[MyReceiver--->A] onReceive : message = " + intent.getStringExtra("message")); }
}
接收者A:Demo_BroadcastReceive_B
Manifest.xml:
<!--
以下权限为自定义广播权限,当该权限被定义时,Demo_BroadcastSend中发出带该权限的广播,
本应用的广播接收器即可接收到该广播 ,而Demo_BroadcastReceive_A 接收不到
-->
<uses-permission android:name="com.sdf.userdefined.receiver.permission" /> <permission
android:name="com.sdf.userdefined.receiver.permission"
android:protectionLevel="normal" />
<!-- 带权限广播接收器 -->
<receiver
android:name="com.sdf.demo_broadcastreceive_b.MyReceiverB"
android:permission="com.sdf.userdefined.send.permission" >
<intent-filter>
<action android:name="com.sdf.userdefined.receiver.action" /> <category android:name="receiver" />
</intent-filter>
</receiver>
Code:
package com.sdf.demo_broadcastreceive_b;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;/**
* 带权限的广播接收器
*
* @author dengfu.su
*
*/
public class MyReceiverB extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
Log.d("sdf", "[MyReceiver--->B] onReceive : intent = " + intent.toString());
Log.d("sdf", "[MyReceiver--->B] onReceive : message = " + intent.getStringExtra("message")); }
}