Android BroadcastReceiver 发送有序广播
普通广播(Normal Broadcast):
一,优缺点:和有序广播的优缺点相反!
二,发送广播的方法:sendBroadcast()
有序广播(Ordered Broadcast):
一,优缺点
优点:1,按优先级的不同,优先Receiver可对数据进行处理,并传给下一个Receiver
2,通过abortBroadcast可终止广播的传播
缺点:效率低
二,发送广播的方法:sendOrderedBroadcast()
三,优先接收到Broadcast的Receiver可通过setResultExtras(Bundle)方法将处理结果存入Broadcast中,
下一个Receiver 通过 Bundle bundle=getResultExtras(true)方法获取上一个 Receiver传来的数据
程序效果:点击按钮,两个Receiver接收同一条广播,在logcat中打印出数据(按照Receiver的优先顺序,Receiver2先,Receiver1后)
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.song"
android:versionCode=""
android:versionName="1.0" > <uses-sdk android:minSdkVersion="" /> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".C48_BroadcastActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!--优先级的设定 MyReceiver2大于MyReceiver1,优先级的范围-~ -->
</activity>
<receiver android:name=".MyReceiver1">
<intent-filter android:priority="">
<action android:name="com.song.123"/>
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver2">
<intent-filter android:priority="">
<action android:name="com.song.123"/>
</intent-filter>
</receiver>
</application> </manifest>
主Activity
package com.song;
//发送广播,bundle绑上key为a的数据
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class C48_BroadcastActivity extends Activity {
/** Called when the activity is first created. */
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent("com.song.123");
Bundle bundle=new Bundle();
bundle.putString("a", "aaa");
intent.putExtras(bundle);
//有序广播
sendOrderedBroadcast(intent, null);
}
}); }
}
Receiver2
package com.song;
//优先接到广播,bundle绑上key为b的数据
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle; public class MyReceiver2 extends BroadcastReceiver{ @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("receiver2");
// context.getSystemService(name);
Bundle bundle=intent.getExtras();
bundle.putString("b", "bbb");
System.out.println("a="+bundle.get("a"));
setResultExtras(bundle);
//切断广播
// abortBroadcast();
} }
Receiver1
package com.song;
//接收从receiver2传来的广播,包含key为a和b的数据
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle; public class MyReceiver1 extends BroadcastReceiver{ @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("receiver1");
//要不要接受上一个广播接收器receiver2传来的的数据
Bundle bundle=getResultExtras(true);
System.out.println("a="+bundle.getString("a")+",b="+bundle.getString("b"));
} }
程序效果
Android BroadcastReceiver 发送有序广播的更多相关文章
- 发送有序广播Ordered Broadcast
import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.vi ...
- android#boardcast#发送自定义广播
广播主要分为两种类型,标准广播和有序广播,通过实践的方式来看下这两种广播具体的区别. 一.发送标准广播 在发送广播之前,我们还是需要先定义一个广播接收器来准备接收此广播才行,不然发出去也是白发.因此新 ...
- Android学习笔记(十二)BroadcastReceiver的有序广播和优先级
前两篇博文中简单整理了普通广播,其实还有有序广播,有序广播在开发中也是比不可少的,可以给广播接收者设定优先级来控制接受顺序,并却可以中断广播传递等等. 一.两种Broadcast: · 普通广播(No ...
- Android(java)学习笔记179:BroadcastReceiver之 有序广播和无序广播(BroadcastReceiver优先级)
之前我们在Android(java)学习笔记178中自定义的广播是无序广播,下面我们要了解一下有序广播: 1. 我们首先了解一下有序广播和无序广播区别和联系? (1) 有序广播> 接受者 ...
- Android(java)学习笔记122:BroadcastReceiver之 有序广播和无序广播(BroadcastReceiver优先级)
之前我们在Android(java)学习笔记178中自定义的广播是无序广播,下面我们要了解一下有序广播: 1. 我们首先了解一下有序广播和无序广播区别和联系? (1)有序广播> 接受者有优先级, ...
- BroadcastReceiver之有序广播
有序广播可以按一定的优先级进行传播 首先进行发送广播 public void click(View v){ Intent intent = new Intent(); intent.setAction ...
- 查看Android 系统发送的广播
命令行输入如下命令 adb shell dumpsys |grep BroadcastRecord
- android BroadcastReceiver
AndroidManifast.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...
- Android——BroadcastReceiver
注释:一般广播不会被阻断,有序广播则会被阻断 注释:这是用动态注册的广播,必须要解绑 xml <?xml version="1.0" encoding="utf-8 ...
随机推荐
- Shell 脚本编程 基本语法:
Shell 脚本编程语法: 注: 文章来源 http://www.cnblogs.com/yunquan/p/6821850.html 视频来源:https://www.bilibili.com/vi ...
- SpringBoot杂记
一.配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: •application.properties •application.yml 配置文件的作用:修改SpringBoo ...
- Vue 中Bus使用
使用:兄弟组件之间传值: 安装: npm install vue-bus 在main.js 中引入vuebus: import Vue from 'vue'; import VueBus from ' ...
- Huawei-R&S-网络工程师实验笔记20190609-VLAN划分综合(Access和Trunk端口)
>Huawei-R&S-网络工程师实验笔记20190609-VLAN划分综合(Access和Trunk端口) >>实验开始,先上拓扑图参考: >>>实验目标 ...
- 解析特殊格式的xml到map
由于项目特殊,需要解析的xml文档样式特别,所以自己写了一个解析特殊xml的方法 先提供xml样式 <?xml version="1.0" encoding="UT ...
- Android:让Link始终保持在程序的WebView中跳转
在Android的WebView中,当点击调用网页的链接时,默认的动作是跳转到系统设定的默认浏览器中.如果想让链接始终在当前WebView中跳转的话,就需要添加以下代码: WebView webVie ...
- PHP中http协议详解
对PHP文件来说 Php能够有 html css javascript php脚本 flash它的不同部分是在不同的地方运行的(server和client) http协议 1. http协议是建 ...
- nyoj 1238 最少换乘 (河南省第八届acm程序设计大赛)
题目1238 题目信息 执行结果 本题排行 pid=1238" style="text-decoration:none; color:rgb(55,119,188)"&g ...
- 開始学习hadoop
思前想后,还是准备自学hadoop,作为一个初级的linux学员,更不懂什么是云.hadoop仅仅知道是个框架和基础平台,详细什么玩意也得慢慢学习了解,但还是明确他的重要性.公司近期也在内部招聘这方面 ...
- saltstack结合Elasticsearch来做salt运行结果展现
salt尽管好用可是机器管理的越来越多,通过cli的结果输出方式查看运行结果越来越多不能满足我的需求.并且作为一个推动运维自己主动化的攻城狮,使用这样的人眼查看运行结果的方式简直土到掉渣.尽管别人看起 ...