发送有序广播

xiaoxiao2021-02-28  70

运行效果图 程序启动后,点击“发送有序广播”按钮,发送一条广播事件,此时观察LogCat窗口中的提示信息, 布局文件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:paddingBottom="@dimen/activity_vertical_margin" android:background="@drawable/stitch_one" tools:context="cn.edu.bzu.orderedbroadcast.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:onClick="send" android:layout_marginTop="50dp" android:text="发送有序广播" android:paddingLeft="5dp" android:paddingRight="5dp" android:background="#FFD202" android:textSize="20sp" /> </RelativeLayout> 添加广播接收者MyBroadcastReceiverOne.java package cn.edu.bzu.orderedbroadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyBroadcastReceiverOne extends BroadcastReceiver { public MyBroadcastReceiverOne() { } @Override public void onReceive(Context context, Intent intent) { Log.i("MyBroadcastReceiverOne","自定义的广播接收者one,接受到了广播事件"); } } 添加广播接收者MyBroadcastReceiverTwo.java package cn.edu.bzu.orderedbroadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyBroadcastReceiverTwo extends BroadcastReceiver { public MyBroadcastReceiverTwo() { } @Override public void onReceive(Context context, Intent intent) { Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接受到了广播事件"); abortBroadcast();//拦截有序广播 Log.i("MyBroadcastReceiverTwo","我是广播接收者Two我的拦截被终止了"); } } 添加广播接收者MyBroadcastReceiverThree.java package cn.edu.bzu.orderedbroadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyBroadcastReceiverThree extends BroadcastReceiver { public MyBroadcastReceiverThree() { } @Override public void onReceive(Context context, Intent intent) { Log.i("MyBroadcastReceiverThree","自定义的广播接收者Three,接受到了广播事件"); } } 界面交互代码MainActivity package cn.edu.bzu.orderedbroadcast; 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); } } 三个广播接收者创建成功之后,需要在清单文件中注册并为它们指定不同的优先级,具体代码如下所示: <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=".MyBroadcastReceiverThree"> <intent-filter android:priority="600"> <action android:name="Intercept_Stitch"/> </intent-filter> </receiver> 上述代码使用android:priority 指定了广播事件的优先级,MyBroadcastReceiverOne优先级最高,其次是MyBroadcastReceiverThree,最低的是MyBroadcastReceiverTwo。 一、广播的类型 1、无序广播           无序广播是一种完全异步执行的广播,在广播发出去之后,所有监听了这个广播事件的广播接收器几乎都会同一时刻接收到这条广播,它们之间没有任何先后顺序可言,这种广播效率会比较高,但同时也意味着它是无法被截断的。工作流程如图所示: 2、有序广播           有序广播是一种同步执行的广播,在广播发出之后,同一时刻只会有一个广播接收器能够接收到这条消息,当这个广播接收器中的逻辑执行完毕后,广播才会继续传递。所以,此时的广播接收器是有先后顺序的,并且可以被拦截。工作流程如图所示: 从图可以看出,有序广播发出一条消息后,高优先级的广播接收器先接收到广播,低优先级的广播接收器后接收到广播。如果高优先级的广播接收器将广播终止,则后面的广播接收器无法接收到广播。优先级就是在清单文件中注册广播接收者时定义的android:priority=""参数,优先级的范围为-1000~1000之间。如果两个广播接收者的优先级相同,则先注册的组件优先接收到广播。拦截广播可以使用abortBroadcast();。
转载请注明原文地址: https://www.6miu.com/read-56334.html

最新回复(0)