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以后广播接收者 ...
随机推荐
- SpringMVC请求@RequestParam中文乱码解决
private String encodeStr(String str) { try { return new String(str.getBytes("ISO-8859-1"), ...
- GitHub上传项目,使用desktop(客户端)教程
GitHub上传项目,使用desktop(客户端)教程 搜索“GitHub上传项目”,能得到很多相关的文章教程,里面讲的都特别麻烦,要弄什么ssh之类的,可算是吓坏了我,使我非常的怀疑为什么GitH ...
- test文件夹,测试类是放在src目录下的,test测试代码是代码啊,当然要放在代码文件夹下
test文件夹,测试类是放在src目录下的,test测试代码是代码啊,当然要放在代码文件夹下 Maven的标准工程结构 Maven的标准工程结构如下: |-- pom.xml(maven的核心配置文件 ...
- DataTables warning: table id=dataTable - Requested unknown parameter 'acceptId' for row 0. For more
重点内容 DataTables warning: table id=dataTable - Requested unknown parameter 'acceptId' for row 0. For ...
- leetcode第一刷_Subsets II
要求子集,有很现成的方法.N个数.子集的个数是2^N.每一个元素都有在集合中和不在集合中两种状态,这些状态用[0,pow(2,N)]中每一个数来穷举,假设这个数中的第i位为1,说明当前集合中包括源数组 ...
- c# 获取一年中的周/根据一年中的第几周获取该周的开始日期与结束日期
/// <summary> /// 获取一年中的周 /// </summary> /// <param name="dt">日期</par ...
- How to resolve unassigned shards in Elasticsearch——写得非常好
How to resolve unassigned shards in Elasticsearch 转自:https://www.datadoghq.com/blog/elasticsearch-un ...
- ES设置查询的相似度算法
similarity Elasticsearch allows you to configure a scoring algorithm or similarity per field. The si ...
- CSS3个人盲点总结【总结中..........】
~:表示同辈元素之后指定类型的元素,如;elm1 ~ elm2表示,elm1之后的所有elm2元素,且elm1与elm2都是在同一个父级元素. +:表示同辈元素的兄弟元素. \A:一个空白换行符 &l ...
- 使用Swing组件编写一个支持中文文本编辑程序ChineseTextEdit.java
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class C ...