Ordered Broadcast有序广播
sendBroadcast()发生无序广播
sendOrderedBroadcast()发送有序广播
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发生广播" /> </LinearLayout>
MainActivity
package com.example.orderbroadcast; import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build; public class MainActivity extends Activity { private Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); send=(Button)this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setAction("dashu.orderedBroadcast");
intent.putExtra("msg", "发送广播");
sendOrderedBroadcast(intent, null);
}
});
}
}
MyReceiver1
package com.example.orderbroadcast; 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) {
// TODO Auto-generated method stub
Toast.makeText(context, "消息内容是"+intent.getStringExtra("msg"), Toast.LENGTH_LONG);
//创建Bundle对象,并存入数据
Bundle bundle=new Bundle();
bundle.putString("frist", "第一个BroadcastReceiver消息...");
//把bundle对象放到广播中传输
setResultExtras(bundle);
//取消Broadcast的继续传播
//abortBroadcast();
} }
MyReceiver
package com.example.orderbroadcast; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast; public class MyReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = getResultExtras(true);
String frist = bundle.getString("frist");
Toast.makeText(context, "第一个Broadcast存入的消息为:" + frist,
Toast.LENGTH_LONG).show();
} }
Mainfest.xml
<receiver android:name="com.example.orderbroadcast.MyReceiver" >
<intent-filter android:priority="10" >
<action android:name="dashu.orderedBroadcast" />
</intent-filter>
</receiver>
<receiver android:name="com.example.orderbroadcast.MyReceiver1" >
<intent-filter android:priority="20" >
<action android:name="dashu.orderedBroadcast" />
</intent-filter>
</receiver>
</application>
android:priority设置接收广播优先级,取值范围-1000 到1000
Ordered Broadcast有序广播的更多相关文章
- 发送有序广播Ordered Broadcast
import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.vi ...
- 17_Android中Broadcast详解(有序广播,无序广播)最终广播,Bundle传递参数,传递参数的时候指定权限
1 Broadcast是Android中的四大组件之一,他的用途很大,比如系统的一些广播:电量低.开机.锁屏等一些操作都会发送一个广播. 2 广播被分为两种不同的类型:"普通广播( ...
- Android学习笔记(十二)BroadcastReceiver的有序广播和优先级
前两篇博文中简单整理了普通广播,其实还有有序广播,有序广播在开发中也是比不可少的,可以给广播接收者设定优先级来控制接受顺序,并却可以中断广播传递等等. 一.两种Broadcast: · 普通广播(No ...
- Zab: A simple totally ordered broadcast protocol(译)
摘要 这是一个关于ZooKeeper正在使用的全序广播协议(Zab)的简短概述.它在概念上很容易理解,也很容易实现,并且提供很高的性能.在这篇文章里,我们会呈现ZooKeeper在Zab上的需求,也会 ...
- Android BroadcastReceiver 发送有序广播
普通广播(Normal Broadcast): 一,优缺点:和有序广播的优缺点相反! 二,发送广播的方法:sendBroadcast() 有序广播(Ordered Broadcast): 一,优缺点 ...
- Android菜鸟的成长笔记(26)——普通广播与有序广播
BroadcastReceiver是Android系统的四大组件之一,BroadcastReceiver是一个全局的系统级监听器,它拥有自己的独立进程. 我们来写一个最简单的广播接收过程 先在mani ...
- BroadcastReceiver之有序广播
有序广播可以按一定的优先级进行传播 首先进行发送广播 public void click(View v){ Intent intent = new Intent(); intent.setAction ...
- 在命令行中通过adb shell am broadcast发送广播通知
通过命令行执行adb shell am broadcast发送广播通知. adb shell am broadcast 后面的参数有:[-a <ACTION>][-d <DATA_U ...
- android86 监听SD卡状态,勒索软件,监听应用的安装、卸载、更新,无序广播有序广播
* 添加权限 <uses-permission android:name="android.permission.RECEIVE_SMS"/> * 4.0以后广播接收者 ...
随机推荐
- pytorch 7 save_reload 保存和提取神经网络
import torch import matplotlib.pyplot as plt # torch.manual_seed(1) # reproducible # fake data x = t ...
- Java并发和多线程2:3种方式实现数组求和
本篇演示3个数组求和的例子. 例子1:单线程例子2:多线程,同步求和(如果没有计算完成,会阻塞)例子3:多线程,异步求和(先累加已经完成的计算结果) 例子1-代码 package cn.fansuni ...
- CSS学习(四)
伪类(Pseudo-classes) CSS伪类是用来添加一些选择器的特殊效果. 伪类的语法: selector:pseudo-class {property:value;} CSS类也可以使用伪类: ...
- [ReactVR] Animate Text, Images, Views, and 3D Elements Using the Animated Library in React VR
Motion is an important aspect of a complete immersive experience, therefor we are going to look into ...
- 编写html经常使用而又easy忘记的语句
设置文件字符编码: <meta charset="utf-8"> 内部样式表: <style type="text/css"> hr { ...
- kqueue演示样例
网络server通常都使用epoll进行异步IO处理,而开发人员通常使用mac,为了方便开发.我把自己的handy库移植到了mac平台上. 移植过程中,网上竟然没有搜到kqueue的使用样例.让我吃惊 ...
- mybatis学习笔记(7)-输出映射
mybatis学习笔记(7)-输出映射 标签: mybatis mybatis学习笔记7-输出映射 resultType 输出简单类型 输出pojo对象和pojo列表 resultMap result ...
- zzulioj--1777--和尚特烦恼3——何时能下山(水题)
1777: 和尚特烦恼3--何时能下山 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 203 Solved: 111 SubmitStatusWeb ...
- OKHttp使用简介
现在android网络方面的第三方库很多,volley,Retrofit,OKHttp等,各有各自的特点,这边博客就来简单介绍下如何使用OKHttp. 梗概 OKHttp是一款高效的HTTP客户端,支 ...
- POJ 3188暴搜
题意: 思路: 裸的暴搜 --. 但是要注意如果你不用所有的按键就能输出最优解的话一定要把所有的字母都安排到一个位置-. 我的一群PE就是这么来的-- 为什么写的人这么少-- // by Sirius ...