发送有序广播
来源:互联网 发布:手机数据永久删除 编辑:程序博客网 时间:2024/06/18 15:04
一:运行效果图
二:1:创建一个名为:“拦截有序广播”的应用程序,布局文件activity_main.xml如下所示:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/stitch_one" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="80dp" android:onClick="send" android:text="发送有序广播" android:paddingLeft="5dp" android:paddingRight="5dp" android:background="#FBFBFF" android:textSize="20sp" /></RelativeLayout>
上述布局文件中定义了一个Button按钮,并为按钮注册了一个点击事件send,当用户点击该按钮时,会发送一条有序广播。
2:MainActivity代码如下:
package cn.edu.bzu.orderbroadcast;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void send (View view){ Intent intent=new Intent(); intent.setAction("Intercept_Stitch"); sendOrderedBroadcast(intent,null); }}
sendOrderedBroadcast(intent,null)用于发送一个有序广播,在该方法中接收两个参数,第1个参数是指定的意图,设置要发送的广播事件。第2个参数指定接收者的权限,如果不想让所有的接收者都看到,可以显式地指定接收者的权限,目前不关心权限可以将其指定为null.
3:添加三个广播接收者MyBroadcastReceiverOne,MyBroadcastReceiverTwo,MyBroadcastReceiverThre
package cn.edu.bzu.orderbroadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyBroadcastReceiverOne extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i("MyBroadcastReceiverOne","自定义的广播接收者one,接收到了广播事件"); }}
package cn.edu.bzu.orderbroadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyBroadcastReceiverTwo extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i("MyBroadcastReceiverTwo","自定义的广播接收者two,接收到了广播事件"); abortBroadcast(); Log.i("MyBroadcastReceiverTwo","我是广播接收者Two,广播被我终结了"); }}
package cn.edu.bzu.orderbroadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyBroadcastReceiverThre extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i("MyBroadcastReceiverThre","自定义的广播接收者thre,接收到了广播事件"); }}
4:三个广播接收者创建成功后,需要在清单文件中注册并为它们指定不同的优先级,清单文件代码如下:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.edu.bzu.orderbroadcast"> <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=".MyBroadcastReceiverOne"> <intent-filter android:priority="1000"> <action android:name="Intercept_Stitch" /> </intent-filter> </receiver> <receiver android:name=".MyBroadcastReceiverTwo"> <intent-filter android:priority="200"> <action android:name="Intercept_Stitch" /> </intent-filter> </receiver> <receiver android:name=".MyBroadcastReceiverThre"> <intent-filter android:priority="600"> <action android:name="Intercept_Stitch" /> </intent-filter> </receiver> </application></manifest>
三个广播接收者中MyBroadcastReceiverOne的优先级最高,其次是MyBroadcastReceiverThre,最低的是MyBroadcastReceiverTwo.
程序启动后,单机“发送有序广播”按钮,发送一条广播事件
优先级最高的MyBroadcastReceiverOne最先收到广播事件,其次是MyBroadcastReceiverThre,最后是MyBroadcastReceiverTwo,说明广播接收者的优先级决定了广播接收的先后顺序。
5:若将广播接收者MyBroadcastReceiverTwo优先级同样设置为1000,并将MyBroadcastReceiverTwo注册在MyBroadcastReceiverOne前面,再来运行程序
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.edu.bzu.orderbroadcast"> <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=".MyBroadcastReceiverTwo"> <intent-filter android:priority="1000"> <action android:name="Intercept_Stitch" /> </intent-filter> </receiver> <receiver android:name=".MyBroadcastReceiverOne"> <intent-filter android:priority="1000"> <action android:name="Intercept_Stitch" /> </intent-filter> </receiver> <receiver android:name=".MyBroadcastReceiverThre"> <intent-filter android:priority="600"> <action android:name="Intercept_Stitch" /> </intent-filter> </receiver> </application></manifest>
此时再来运行程序:
MyBroadcastReceiverTwo最先接收到了广播事件,其次是MyBroadcastReceiverOne,这说明当两个广播接收者优先级相同时,先注册的广播接收者会先接收到广播事件。
6:修改MyBroadcastReceiverTwo如下
package cn.edu.bzu.orderbroadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyBroadcastReceiverTwo extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i("MyBroadcastReceiverTwo","自定义的广播接收者two,接收到了广播事件"); abortBroadcast(); Log.i("MyBroadcastReceiverTwo","我是广播接收者Two,广播被我终结了"); }}
运行程序:
只有MyBroadcastReceiverTwo接收到了自定义的广播事件,其他的广播接收者都没有接收到,因此说明广播被MyBroadcastReceiverTwo终止了。
- 广播接收者-发送有序广播
- 广播接收者---发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- Android 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 关于Win10创意者更新之后蓝屏的修复办法
- 解决Gradle DSL method not found: 'compile()'错误的方法
- struts2
- Android插件化:从入门到放弃
- mysql 存储过程 通过存储过程的方式简化sql操作
- 发送有序广播
- python生成器的表达式形式
- 27. Remove Element
- 深度学习 —— 卷积神经网路 CNN
- 如何确定Kafka的分区数、key和consumer线程数
- Caffe——python接口学习(2):生成solver文件
- oracle 11g 服务端下载地址及安装说明
- 【模板】Splay
- 448. Find All Numbers Disappeared in an Array