Android——有序广播和无序广播
来源:互联网 发布:林小宅的淘宝店叫什么 编辑:程序博客网 时间:2024/05/23 19:21
1.有序广播
有序广播是一种同步执行的广播,在广播发出之后,同一时刻只会有一个广播接收器能够收到这条广播消息,当这个广播接收器中的逻辑执行完毕后,广播才会继续传递。此时的广播接收器是有先后顺序,优先级高的广播接收器就可以先收到广播消息,并且前面的广播接收器还可以截断正在传递的广播,这样后面的广播接收器就无法收到广播消息了。想要拦截一条广播不往下发送,可以使用abortBroadcast();方法。
2.无序广播
无序广播是一种完全异步执行的广播,在广播发出去之后,所有的广播接收器几乎同时接收到这条广播消息,但是接受的顺序不确定,这种广播的效率比较高,但也意味着它是无法被截断的。
下面通过一个例子展示一下有序广播:(1),任务介绍:
(2)布局设置:
<?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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="@drawable/stitch_one" tools:context="com.edu.bzu.mybroder.MainActivity"> <Button android:text="发送有序广播" android:textSize="20dp" android:onClick="send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="97dp" android:layout_marginStart="97dp" android:layout_marginTop="75dp" android:id="@+id/button" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /></RelativeLayout>(3)点击发送有序广播按钮时间处理:
代码如下:
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); }}(4).分别创建三个广播接受者,
MyReceiver,
MyReceiverTwo,MyReceiverThree。打印日志用于区分:
public class MyReceiver extends BroadcastReceiver { public MyReceiver() { } @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i("MyReceiver","自定义的广播接收者,接收到了广播事件"); throw new UnsupportedOperationException("Not yet implemented"); }}public class MyReceiverTwo extends BroadcastReceiver { public MyReceiverTwo() { } public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. throw new UnsupportedOperationException("Not yet implemented"); }}public class MyReceiverThree extends BroadcastReceiver { public MyReceiverThree() { } public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. throw new UnsupportedOperationException("Not yet implemented"); }}(5)最后,需要在清单文件中注册:通过设置priority的值,来确定每个接受者的优先级,priority的值越大,它的优先级就越大,优先级的范围在-1000到1000之间,如果两个广播接受者的优先级相同,则先注册的组件优先接收到广播。如果两个应用程序监听了同一个广播事件并设置了优先级,则先安装的应用的优先接收到广播。<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.edu.bzu.mybroder"> <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"> <intent-filter android:priority="100"> <action android:name="Intercept_Stitch" /> </intent-filter> </receiver> <receiver android:name=".MyReceiverTwo" android:enabled="true" android:exported="true"> <intent-filter android:priority="200"> <action android:name="Intercept_Stitch" /> </intent-filter> </receiver> <receiver android:name=".MyReceiverThree" android:enabled="true" android:exported="true"> <intent-filter android:priority="600"> <action android:name="Intercept_Stitch" /> </intent-filter> </receiver> </application></manifest>
阅读全文
0 0
- Android——有序广播和无序广播
- Android中有序广播和无序广播
- 有序广播和无序广播
- 有序广播和无序广播
- 有序广播和无序广播
- 有序广播和无序广播
- 有序广播和无序广播
- 有序广播和无序广播
- 有序广播和无序广播
- 有序广播和无序广播
- 有序广播和无序广播
- android有序广播和无序广播的区别
- android有序广播和无序广播的区别
- android有序广播和无序广播的区别
- android有序广播和无序广播的区别
- Android有序广播和无序广播的区别
- android有序广播和无序广播的区别
- android有序广播和无序广播的区别
- C++ const相关
- 十三周 Dynamic programming counting bits
- CentOS 7中源码安装MySQL 5.7.6+详细教程
- hdu 2141
- AWT高仿计算器
- Android——有序广播和无序广播
- Windows系统中设置定时开关机方法
- MySQL查询语句执行的过程
- Unity 使用Photon Server 创建一个简单聊天室
- 工作流引擎Activiti学习 --- 小小总结
- 机器学习通俗入门-使用梯度下降法解决最简单的线性回归问题
- AndroidStudio TODO
- 1.串口通信(转)
- 【报错】Duplicate entry '0' for key 'PRIMARY'