Android案例:通过简单的小案例理解有序广播的传递以及怎样将其拦截

xiaoxiao2021-02-28  104

一、运行效果图:

二、用到的布局文件和class类

三、实现代码以及分析

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: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="60dp" android:layout_centerHorizontal="true" android:onClick="send" android:layout_marginTop="50dp" android:text="发送有序广播" android:textSize="20sp" />

</RelativeLayout>

2、MainActivity的代码如下所示:
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)用于发送一个有序广播,在该方法中接收两个参数,第一个参数是指定的意图,设置要发送的广播事件是"Intercept_Stitch",第二个参数是指定接受者的权限,目前不关心权限可以将其指定为null。 -------------------------------------------我是2和3的分割线----------------------------------------------

      有序广播是同步执行的广播,也就是说在广播发出之后,同一时刻只有一个广播接收器才能够接收到这条信息,因此添加三个广播接收器MyBroadcastReceiverOne、MyBroadcastReceiverTwo、MyBroadcastReceiverThree,通过它们不同的打印信息来看有序广播是如何进行的,以及怎样达到我们想要的有序进行。

3、MyBroadcastReceiverOne的代码如下所示:

public class MyBroadcastReceiverOne extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i("MyBroadcastReceiverOne","自定义的广播接收者one,接收到了广播事件"); } }

MyBroadcastReceiverTwo、MyBroadcastReceiverThree的代码与MyBroadcastReceiverOne相同,只修改相应编号的打印信息就行。

4、三个广播接收者创建好以后,在清单文件中注册并为它们指定不同的优先级,      清单文件代码如下: <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=" " 表示广播接收器的优先级,优先级的范围为-1000~1000之间。 5、运行程序,打印显示的信息如下: 分析:打印信息显示,广播接收者One先接收到了广播事件,其次是Three,最后是Two,也就是说优先级高的先接收到了广播事件,广播向下传递,  第二优先级高的再接收到,再向下传递,以此类推。。。 那如果有相同优先级的广播接收者怎么办?????? 实验代码如下: <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=".MyBroadcastReceiverThree"> <intent-filter android:priority="600"> <action android:name="Intercept_Stitch"/> </intent-filter> </receiver>将two的优先级改成和one一样高的1000,并将two在one的前面先注册,运行效果如下: 分析:打印信息显示,two先接收到了广播事件,其次是one,最后是three。。。当你多测试几组数据时,你会发现,相同的优先级,先注册的广播接受者优先接收到了广播,所以总结起来就是,优先级别相同的,先注册的组件先接收到广播, 若在two中加入下述代码呢???: public class MyBroadcastReceiverTwo extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件"); abortBroadcast(); Log.i("MyBroadcastReceiverTwo","我是广播接收者Two,广播被我终止了"); } } 运行程序,打印信息如下: abortBroadcast()语句用于终止有序广播,当程序执行完这句代码后,广播事件将会被终结,不再继续向下传递,one和three不再接收到广播事件,相应的信息不再被打印出。
转载请注明原文地址: https://www.6miu.com/read-58337.html

最新回复(0)