发送有序广播
来源:互联网 发布:盘锦兼职淘宝客服招聘 编辑:程序博客网 时间:2024/05/17 23:59
1.编写用户交互界面
<?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="com.example.bz0209.myapplication.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="send" android:text="发送有序广播" android:layout_marginTop="80dp" android:layout_centerHorizontal="true" android:paddingLeft="5dp" android:paddingRight="5dp" android:background="#FBFBFF" android:textSize="20sp" /></RelativeLayout>
2.点击发送有序广播按钮时事件处理
package com.example.bz0209.myapplication;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.创建广播接收者MyBroadcastReceiverOne,MyBroadcastReceiverTwo,MyBroadcastReceiverThree
package com.example.bz0209.myapplication;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) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i("MyBroadcastReceiverOne","自定义的广播接收者One,接收到了广播事件"); }}
package com.example.bz0209.myapplication;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) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件"); }}
package com.example.bz0209.myapplication;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) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i("MyBroadcastReceiver 3","自定义的广播接收者Three,接收到了广播事件"); }}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>5.运行结果如图:
另一个就是将MyBroadcastReceiverTwo的优先级改为1000,同时拦截有序广播package com.example.bz0209.myapplication;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) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件"); abortBroadcast(); Log.i("MyBroadcastReceiverTwo","我是广播接收者Two,广播被我终结了"); }}结果如图所示,没有传播到MyBroadcastReceiverThree,广播就被终结了,所以MyBroadcastReceiverThree没有收到广播
阅读全文
0 0
- 广播接收者-发送有序广播
- 广播接收者---发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- Android 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- 发送有序广播
- js中自定义的map.js
- Android Runtime.exec()文件名带空格不生效
- 《近匠》专访GE 秦川、唐翊国——Predix工业物联网平台带来了什么
- Struts,Spring,Hibernate三大框架的面试与笔试题
- JedisPool
- 发送有序广播
- SpringAOP
- nginx 编译安装
- (0002) H5开发之Frame框架理解
- 商业智能
- Redhat5.9安装qt5.5.1出错error while loading shared libraries:libX11-cxb.so.1
- bootstrap-动态表单
- linux驱动编写过程中遇到的几个问题及解决办法
- C++ 查找算法-有序集合