Android 广播 Android 中在有序广播中添加自定义权限的实例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android 广播 Android 中在有序广播中添加自定义权限的实例

  2021-03-24 我要评论
想了解Android 中在有序广播中添加自定义权限的实例的相关内容吗,在本文为您仔细讲解Android 广播的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,中在有序广播添加权限,Android,有序广播,下面大家一起来学习吧。

Android 中在有序广播中添加自定义权限的实例

前言;

有序广播说明:

有序广播因为要处理消息的处理结果,所以要复杂一些。

* sendOrderedBroadcast(Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras);

如果只是想让广播可以按优先级来收取,并不在意处理的结果,可以用下面的版本:

* sendOrderedBroadcast(Intent intent, String receiverPermission);

同样,在多用户环境下,也可以选择给哪个用户发广播:

* sendOrderedBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras);

首先我们要在AndroidManifest.xml中自定义一个权限,格式参考系统自带的权限,Android.permission.XXXXX,只要是xxx.peimission.XXXXXX就行,如果不按照这个格式,那么这个权限可能没法使用。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  package="com.example.lsj.broadcasttest"> 
  <span style="color:#FF0000;"> <permission 
    android:name="test.permission.TEST" 
    android:protectionLevel="normal" 
    ></permission></span> 
  <application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
      <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
 
        <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
    </activity> 
 
    <receiver 
      android:name=".MyReceiver" 
      android:enabled="true" 
      android:exported="true"> 
      <intent-filter android:priority="20"> 
        <action android:name="hahaha" /> 
      </intent-filter> 
    </receiver> 
    <receiver 
      android:name=".MyReceiver2" 
      android:enabled="true" 
      android:exported="true"> 
      <intent-filter android:priority="19"> 
        <action android:name="hahaha" /> 
      </intent-filter> 
 
    </receiver> 
  </application> 
 
  <span style="color:#FF0000;"> <uses-permission android:name="test.permission.TEST"/></span> 
</manifest> 

然后使用sendOrderedBroadcast(intent,"test.permission.TEST");就可以发送有序广播了,当然发送广播之前还要指定一下接受者的优先级,优先级越高,android:priority指定的数字就越大。取值的范围是:-1000~1000这个就不详细叙述了。

public class MainActivity extends AppCompatActivity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button send= (Button) findViewById(R.id.send); 
    send.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        Intent intent=new Intent(); 
        intent.setAction("hahaha"); 
 
        intent.putExtra("msg","一个简单的消息"); 
        sendOrderedBroadcast(intent,"test.permission.TEST"); 
      } 
    }); 
  } 
} 

第一个BroadcastReceiver在接收到广播的时候,如果想要添加一些自己的东西进去,可以先创建一个Bundle对象,并且存入数据。
然后通过setResultExtras(bundle),把这个bundle添加到原来的消息中,

ublic class MyReceiver extends BroadcastReceiver { 
  public MyReceiver() { 
  } 
 
  @Override 
  public void onReceive(Context context, Intent intent) { 
    Toast.makeText(context,"接收到的Intent的Action为:"+intent.getAction()+"\n消息内容是:"+intent.getStringExtra("msg"),Toast.LENGTH_LONG).show(); 
    Bundle bundle=new Bundle(); 
    bundle.putString("first","第一个BroadCast存入的消息!"); 
    setResultExtras(bundle); 
 
  } 
} 

下一个BroadcastReceiver通过getResultExtras可以将信息提取出来。

ublic class MyReceiver2 extends BroadcastReceiver { 
  public MyReceiver2() { 
  } 
 
  @Override 
  public void onReceive(Context context, Intent intent) { 
    // TODO: This method is called when the BroadcastReceiver is receiving 
    Bundle bundle=getResultExtras(true); 
    String first=bundle.getString("first"); 
    Toast.makeText(context,"第一个BroadCast存入的消息为:"+first,Toast.LENGTH_LONG).show(); 
  } 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们