发送有序广播Ordered Broadcast
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SortedBroadcast extends Activity {
Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sorted_broadcast);
//获取程序中的send按钮
send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 创建Intent对象
Intent intent = new Intent();
intent.setAction(broadcasttest.action.CRAZY_BROADCAST");
intent.putExtra("msg", "简单的消息");
//发送有序广播
sendOrderedBroadcast(intent, null);
}
});
}
}
对于有序广播而言,它会按优先级依次触发每个BroadcastReceiver的onReceive()方法。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class MyReceiver1 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "接收到的Intent的Action为:"+
intent.getAction()+"\n消息内容是:"+intent.getStringExtra("msg"),
5000).show();
//创建一个Bundle对象,并存入数据
Bundle bundle = new Bundle();
bundle.putString("first", "第一个BroadcastReceiver存入的信息");
//将Bundle放入结果中
setResultExtras(bundle);
//取消Broadcast的继续传播
abortBroadcast();
}
}
上面的BroadcastReceiver不仅处理了它接收到的消息,而且向处理结果中存入了key为first的消息,这个消息将可以被第二个BroadcastReceiver解析出来。,不过最后一行代码用于取消广播,如果保持这条代码生效,那么优先级比MyReceiver1低的BroadcastReceiver都将不会被触发。
在AndroidManifest.xml文件中部署该BroadcastReceiver,并指定其优先级为20,配置片段如下:
<receiver android:name=".MyReceiver1">
<intent-filter android:priority="20">
<action android:name="broadcasttest.action.CRAZY_BROADCAST"/>
</intent-filter>
</receiver>
接下来再为程序提供第二个BroadcastReceiver,这个BroadcastReceiver将会解析前一个BroadcastReceiver所存入的key为first的消息,
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class MyReceiver2 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = getResultExtras(true);
//解析前一个BroadcastReceiver所存入的key为first的消息
String first = bundle.getString("first");
Toast.makeText(context, "第一个Broadcast存入的消息为:"+first,5000).show();
}
}
在AndroidManifest.xml文件中部署该BroadcastReceiver,并指定其优先级为0,配置片段如下:
<receiver android:name=".MyReceiver2">
<intent-filter android:priority="0">
<action android:name="broadcasttest.action.CRAZY_BROADCAST"/>
</intent-filter>
</receiver>
发送有序广播Ordered Broadcast的更多相关文章
- Android BroadcastReceiver 发送有序广播
普通广播(Normal Broadcast): 一,优缺点:和有序广播的优缺点相反! 二,发送广播的方法:sendBroadcast() 有序广播(Ordered Broadcast): 一,优缺点 ...
- Ordered Broadcast有序广播
sendBroadcast()发生无序广播 sendOrderedBroadcast()发送有序广播 activity_main.xml <LinearLayout xmlns:android= ...
- 17_Android中Broadcast详解(有序广播,无序广播)最终广播,Bundle传递参数,传递参数的时候指定权限
1 Broadcast是Android中的四大组件之一,他的用途很大,比如系统的一些广播:电量低.开机.锁屏等一些操作都会发送一个广播. 2 广播被分为两种不同的类型:"普通广播( ...
- Android 发送自定义广播(标准和本地)
1.首先自定义一个广播接收器:MyBroadcastReceiver package example.com.mybroadcastreceiver; import android.content.B ...
- android: 发送自定义广播
5.3.1 发送标准广播 在发送广播之前,我们还是需要先定义一个广播接收器来准备接收此广播才行,不然发 出去也是白发.因此新建一个 MyBroadcastReceiver 继承自 Broadca ...
- Android学习总结(六)———— 发送自定义广播
一.两种广播类型 2.1 标准广播 是一种完全异步执行的广播,在广播发出去之后,所有的广播接收器几乎都会在同一时刻接收到这条广播消息,因此它们之间没有任何先后顺序可言.这种广播的效率会比较高,但同时也 ...
- Android菜鸟的成长笔记(26)——普通广播与有序广播
BroadcastReceiver是Android系统的四大组件之一,BroadcastReceiver是一个全局的系统级监听器,它拥有自己的独立进程. 我们来写一个最简单的广播接收过程 先在mani ...
- android#boardcast#发送自定义广播
广播主要分为两种类型,标准广播和有序广播,通过实践的方式来看下这两种广播具体的区别. 一.发送标准广播 在发送广播之前,我们还是需要先定义一个广播接收器来准备接收此广播才行,不然发出去也是白发.因此新 ...
- android86 监听SD卡状态,勒索软件,监听应用的安装、卸载、更新,无序广播有序广播
* 添加权限 <uses-permission android:name="android.permission.RECEIVE_SMS"/> * 4.0以后广播接收者 ...
随机推荐
- Android ActionBar以及menu的代码设置样式
menu部分xml代码 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android= ...
- Mysql数据库实践操作之————批量插入数据(100万级别的数据)
第一种方法:使用insert into 插入 从Redis每次获取100条数据,根据条件去插入到Mysql数据库中: 条件: 如果当前队列中的值大于1000条,则会自动的条用该方法,该方法每次获取从队 ...
- Win7x64_chromeX86_相关路径
1. C:\Users\33\AppData\Local\Google 里面有2个文件夹:“Chrome”.“CrashReports” 2. C:\Program Files (x86)\Googl ...
- JavaScript的严格模式
js除了在普通的常规模式,ECMAscript 5添加了第二种运行模式:"严格模式"(strict mode).严格模式支持IE9+ Chrome FireFox 等主流浏览器. ...
- go切片
本文实例讲述了GO语言数组和切片的用法.分享给大家供大家参考.具体分析如下: 一.数组 与其他大多数语言类似,Go语言的数组也是一个元素类型相同的定长的序列. (1)数组的创建. 数组有3种创建方式: ...
- 对List里的对象元素进行排序
public class Student { private int studentId; private String studentName; private int age; public St ...
- Extjs中renderer:function函数用法
renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){ } 1.value是当前单元格的值 2.cellme ...
- iOS开发 字符串MD5加密
/*** MD5 ***/ #define CC_MD5_DIGEST_LENGTH 16 /* digest length in bytes */ #define CC_MD ...
- (转)TCP、UDP、IP协议
原文地址:http://blog.chinaunix.net/uid-26833883-id-3627644.html 互连网早期的时候,主机间的互连使用的是NCP协议.这种协议本身有很多缺陷,如 ...
- git tag推送小分析
一个推送可以用三条命令 -[deleted]-git push origin --tags git push origin master --follow-tags git push --follow ...