Android中发送有序广播案例代码
android系统提供了两种广播类型,一种是有序广播,一种是有序广播。
(1)无序广播是完全异步执行,发送广播时所有监听这个广播的广播接收者都会收到此消息,但接收的顺序不确定。
(2)有序广播是按照接收者的优先级接收,只有一个广播接收者能接收信息,在此广播接收者中逻辑执行完毕后,才会继续传递。
实验要求:通过sendorderedbroadeast()发送一条有序广播
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="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>
2.在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); } }
3.创建三个广播接收者
(1)mybroadcastreceiverone
(2)mybroadcastreceivertwo
(3)mybroadcastreceiverthree
(1)在mybroadcastreceiverone中代码如下:
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,接收到了广播事件"); } }
(2)在mybroadcastreceivertwo中代码如下:
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,接收到了广播事件"); } }
(3)在mybroadcastreceiverthree中代码如下:
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,接收到了广播事件"); } }
4.在配置文件androidmanifest中代码如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.edu.bzu.orderedbroadcast"> <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=".mybroadcastreceiverthree"> <intent-filter android:priority="600"> <action android:name="intercept_stitch"/> </intent-filter> </receiver> </application> </manifest>
5.实验效果图:
运行程序后解决如下问题:
问题1:程序启动后,单击“发送有序广播”按钮,发出一条广播事件,此时观察logcat窗口下的提示信息,输出什么?为什么?
点击按钮后出现如下图所示提示信息,原因是有序广播根据优先级接收广播信息的,在配置文件中广播接收者one的priority值最大,所以它先接收到广播,其次是three,最后是two。其中android:priority="值"是用来设置广播接收者的优先级的,值越大,优先级别越高。
问题2:若将广播接收者mybroadcastreceivertwo优先级同样设置为1000,并将mybroadcastreceivertwo注册在mybroadcastreceiverone前面,再来运行程序,观察结果,你能得出什么结论?
修改配置文件androidmanifest中代码如下:
<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>
运行程序出现如下图所示提示信息,原因是当广播接收者的优先级别相同时,先注册的广播接收者先接收到广播。
问题3:修改mybroadcastreceivertwo如下,观察结果,你又可以得出什么结论?
public class mybroadcastreceivertwo extends broadcastreceiver { public mybroadcastreceivertwo() { } @override public void onreceive(context context, intent intent) { log.i("mybroadcastreceivertwo","自定义的广播接收者two,接收到了广播事件"); abortbroadcast();//有序广播拦截器 log.i("mybroadcastreceivertwo","我是广播接收者two,广播被我终止了"); } }
运行程序出现如下图所示提示信息,原因是广播接收者two的优先级最高,所以在two中写一个拦截器,优先级低的广播接收者将接收不到广播信息,所以one和three没有接收到广播事件,也就没有打印关于它们的信息。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。