Android冷知识(3)自定义权限广播

网上资料也很多,测试的时候大部分都不可运行,差点意思,所以这里自己整理了一份。

首先,自定义广播 发送端

<!-- 自定义的兄弟广播权限(只有符合这个权限的广播才可以接收处理) -->
    <uses-permission android:name="com.brother.RECEIVE" /> 
    <permission android:name="com.brother.RECEIVE" android:protectionLevel="normal"> </permission>
        Intent intent = new Intent();
        intent.setAction("action.kkkkkkkkk");
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); //设置为前台广播
        sendBroadcast(intent,"com.brother.RECEIVE");    

然后,接收端

<!-- 自定义的兄弟广播权限(只有符合这个权限的广播才可以接收处理) -->
    <uses-permission android:name="com.brother.RECEIVE"/> 
    <permission android:name="com.brother.RECEIVE" android:protectionLevel="normal"> </permission>
    <!--这里的第二个权限没测试是否可以去掉-->
 <!-- 自定义广播接收器 (permission="com.brother.RECEIVE")-->
        <receiver
            android:name="com.test.KKReceiver"
            android:exported="true"
            android:label="@string/app_name"
            android:permission="com.brother.RECEIVE">
            <intent-filter>
                <action android:name="action.kkkkkkkkk" />
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
public class KKReceiver extends BroadcastReceiver{

    private String TAG = KKReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context ctx, Intent intent) {
        if(intent.getAction().equals("action.kkkkkkkkk")){
            Log.e("进入报告广播", "此时的测试状态是:");
            String name = intent.getStringExtra("name");
        ...
        }

    }

这就是所有代码了,不多就不传demo了。之前写过一篇类似的但是自己回头测试的时候有点逻辑问题,现在补一篇。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值