Android案例:通过简单的小案例理解有序广播的传递以及怎样将其拦截
2017-06-07 18:37
716 查看
一、运行效果图:
二、用到的布局文件和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不再接收到广播事件,相应的信息不再被打印出。
相关文章推荐
- Android--广播的类型及拦截有序广播案例
- android 发送有序广播sendOrderBraodCast、截断广播和广播之间的数据传递
- Android通过POST方式传递数据到服务器简单demo
- Android 进程常驻(5)----开机广播的简单守护以及总结
- android 学习笔记8-广播接收者 有序 无序广播 案例
- 通过简单案例转账业务引出“事务”以及JAVAEE三层架构
- android,自定义广播,最终广播接收者,防止拦截广播,有序广播,无序广播
- Android 进程常驻(5)----开机广播的简单守护以及总结
- PetShop4.0视频讲解 - 通过简单案例理解petshop4.0的工厂模式
- 安卓通过有序广播拦截特定电话!
- Android四大组件——BroadcastReceiver普通广播、有序广播、拦截广播、本地广播、Sticky广播、系统广播
- 通过小案例简单理解递归算法
- 17_Android中Broadcast详解(有序广播,无序广播)最终广播,Bundle传递参数,传递参数的时候指定权限
- android 发送有序广播sendOrderBraodCast、截断广播和广播之间的数据传递
- 在通过android的广播机制拦截短信的实现过程中遇到的问题
- android 发送有序广播sendOrderBraodCast、截断广播和广播之间的数据传递
- Android-有序广播简单例子
- 对Android为intent提供两种传递对象参数类型的方法的个人简单理解
- 简单理解laravel框架中的服务容器,服务提供者以及怎样调用服务
- Android 进程常驻(5)----开机广播的简单守护以及总结