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了。之前写过一篇类似的但是自己回头测试的时候有点逻辑问题,现在补一篇。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在安卓开发中,可以通过广播申请自定义权限。以下是一些基本步骤: 1. 在AndroidManifest.xml文件中添加自定义权限声明: ``` <permission android:name="com.example.custompermission" android:protectionLevel="signature"/> ``` 其中,android:name是自定义权限的名称,android:protectionLevel指定了权限保护级别。 2. 在BroadcastReceiver中发送自定义广播,并附带自定义权限: ``` Intent intent = new Intent("com.example.ACTION_CUSTOM_BROADCAST"); intent.putExtra("message", "Hello, world!"); intent.addPermission("com.example.custompermission"); sendBroadcast(intent); ``` 其中,"com.example.ACTION_CUSTOM_BROADCAST"是自定义广播的名称,"com.example.custompermission"是自定义权限的名称。 3. 在需要使用自定义权限的地方,检查是否拥有该权限: ``` if (ContextCompat.checkSelfPermission(this, "com.example.custompermission") == PackageManager.PERMISSION_GRANTED) { // 执行需要权限的操作 } else { // 申请权限 ActivityCompat.requestPermissions(this, new String[]{"com.example.custompermission"}, 1); } ``` 其中,"this"是当前上下文,"com.example.custompermission"是自定义权限的名称,1是申请权限时的请求码。 4. 在权限申请结果回调中处理申请结果: ``` @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (requestCode == 1) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // 执行需要权限的操作 } else { // 权限申请被拒绝 } } } ``` 其中,1是申请权限时的请求码,PackageManager.PERMISSION_GRANTED表示权限已授权,PackageManager.PERMISSION_DENIED表示权限被拒绝。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值