文章标题 拦截有序广播
来源:互联网 发布:巴黎文华东方酒店知乎 编辑:程序博客网 时间:2024/05/29 08:29
拦截有序广播程序对应的布局文件
<?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:background="@drawable/stitch_one" tools:context="cn.edu.bzu.bordercast3.MainActivity"><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="80dp" android:onClick="send" android:background="#f2d2f1f4" android:text="发送有序广播" android:textSize="20sp"/></RelativeLayout>
上述布局文件中,定义一个Button按钮,并为按钮注册了点击事件send,当用户点击该按钮时,会发送一条有序广播。
MainActivitty代码
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); }}
sendOrderBordercast(intent,null)用于发送一个有序广播,第一个参数指定意图,设置要发送的广播事件,第二个参数制定接受者的权限,如果不想让所有的接受者都看到,可以显式的指定接受者,不关心权限可以将其指定为null。
添加广播接收者
广播接收者1
public class MyReceiverone extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i(" MyReceiverone","自定义广播接收者one,接受到了广播事件"); }}
广播接收者2
public class MyReceiverTwo extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i(" MyReceivertwo","自定义广播接收者two,接受到了广播事件"); }}
广播接收者3
public class MyReceiverthree extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i(" MyReceiverthree","自定义广播接收者three,接受到了广播事件"); }}
清单文件
<receiver android:name=".MyReceiverone"> <intent-filter android:priority="1000"> <action android:name="Intercept_Stitch"/> </intent-filter> </receiver> <receiver android:name=".MyReceiverTwo"> <intent-filter android:priority="200"> <action android:name="Intercept_Stitch"/> </intent-filter> </receiver> <receiver android:name=".MyReceiverthree"> <intent-filter android:priority="600"> <action android:name="Intercept_Stitch"/> </intent-filter> </receiver>
修改MyBroadcastReceiverTwo如下
public class MyReceiverTwo extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i(" MyReceivertwo","自定义广播接收者two,接受到了广播事件"); abortBroadcast(); Log.i(" MyReceivertwo","我是广播接收者two,广告被我终结了"); }}
若将广播接收者MyBroadcastReceiverTwo优先级同样设置为1000,并将MyBroadcastReceiverTwo注册在MyBroadcastReceiverOne前面
<receiver android:name=".MyReceiverTwo"> <intent-filter android:priority="1000"> <action android:name="Intercept_Stitch"/> </intent-filter> </receiver> <receiver android:name=".MyReceiverone"> <intent-filter android:priority="1000"> <action android:name="Intercept_Stitch"/> </intent-filter> </receiver> <receiver android:name=".MyReceiverthree"> <intent-filter android:priority="600"> <action android:name="Intercept_Stitch"/> </intent-filter> </receiver>
广播的优先级相同时,先注册的广播会先接受广播事件
阅读全文
0 0
- 文章标题 拦截有序广播
- 拦截有序广播案例
- 拦截有序广播
- 拦截有序广播
- 案例-----拦截有序广播
- 案例---拦截有序广播
- 拦截一条有序广播
- 拦截有序广播
- 案例--拦截有序广播
- 有序广播拦截
- 拦截有序广播
- 有序广播拦截
- 广播--案例:拦截有序广播
- 实现拦截一条有序广播
- 实现拦截一条有序广播
- 实现拦截一条有序广播
- Android--有序广播的实例解析--案例《拦截有序广播》
- 有序广播的实例解析--android案例《拦截有序广播》
- hdu-4535-吉哥系列故事——礼尚往来-组合数学-错排-java
- Kotlin语法
- 关于Git以及几种远程库的介绍
- OpenWrt嵌入LUCI
- “早知道这些我的公司就不会死”系列(二):Cohort Analysis
- 文章标题 拦截有序广播
- Apache反向代理设置
- 抽象类与接口
- DateFormatUtils的用法
- android kotlin基础(一)-新建项目
- golang通道中传递slice数据的入坑经验
- 第二行代码学习笔记——第十三章:进阶之路——你还应该掌握的高级技巧
- Java注解之Retention、Documented、Inherited介绍
- Playframework常用命令