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有序广播的更多相关文章

  1. 发送有序广播Ordered Broadcast

    import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.vi ...

  2. 17_Android中Broadcast详解(有序广播,无序广播)最终广播,Bundle传递参数,传递参数的时候指定权限

     1  Broadcast是Android中的四大组件之一,他的用途很大,比如系统的一些广播:电量低.开机.锁屏等一些操作都会发送一个广播. 2  广播被分为两种不同的类型:"普通广播( ...

  3. Android学习笔记(十二)BroadcastReceiver的有序广播和优先级

    前两篇博文中简单整理了普通广播,其实还有有序广播,有序广播在开发中也是比不可少的,可以给广播接收者设定优先级来控制接受顺序,并却可以中断广播传递等等. 一.两种Broadcast: · 普通广播(No ...

  4. Zab: A simple totally ordered broadcast protocol(译)

    摘要 这是一个关于ZooKeeper正在使用的全序广播协议(Zab)的简短概述.它在概念上很容易理解,也很容易实现,并且提供很高的性能.在这篇文章里,我们会呈现ZooKeeper在Zab上的需求,也会 ...

  5. Android BroadcastReceiver 发送有序广播

    普通广播(Normal Broadcast): 一,优缺点:和有序广播的优缺点相反! 二,发送广播的方法:sendBroadcast() 有序广播(Ordered Broadcast): 一,优缺点 ...

  6. Android菜鸟的成长笔记(26)——普通广播与有序广播

    BroadcastReceiver是Android系统的四大组件之一,BroadcastReceiver是一个全局的系统级监听器,它拥有自己的独立进程. 我们来写一个最简单的广播接收过程 先在mani ...

  7. BroadcastReceiver之有序广播

    有序广播可以按一定的优先级进行传播 首先进行发送广播 public void click(View v){ Intent intent = new Intent(); intent.setAction ...

  8. 在命令行中通过adb shell am broadcast发送广播通知

    通过命令行执行adb shell am broadcast发送广播通知. adb shell am broadcast 后面的参数有:[-a <ACTION>][-d <DATA_U ...

  9. android86 监听SD卡状态,勒索软件,监听应用的安装、卸载、更新,无序广播有序广播

    * 添加权限 <uses-permission android:name="android.permission.RECEIVE_SMS"/> * 4.0以后广播接收者 ...

随机推荐

  1. MyEclipse报错Access restriction: The type BASE64Encoder is not accessible due to restriction on required library

    错误截图: 解决办法: 1.进入Project --> Properties --> Java Build Path --> Libraries 2.remove 掉 JRE Sys ...

  2. 自己动手写shell命令之more

    unix下more命令的简单实现: #include <stdio.h> #define PAGELEN 24 #define LINELEN 512 int do_more(FILE * ...

  3. 设计模式实例(Lua)笔记之七(Decorator模式)

    1.描写叙述 就说说"我"上小学的的糗事吧. 我上小学的时候学习成绩非常的差,班级上 40 多个同学,我基本上都是在排名 45 名以后,依照老师给我的定义就是"不是读书的 ...

  4. JDBC连接mysql时出现的ssl问题

    使用MySQL数据库时出现如下错误: WARN: Establishing SSL connection without server's identity verification is not r ...

  5. eclipse 设置代码大小和布局里面代码大小

    Eclipse字体大小调整: Window /  Preferences / General / Appearance / ColorsAnd Fonts .在右边的对话框里选择Java – Java ...

  6. Find Blank Cell in Excel

    Click Home > Find & Select > Go To Special. In the Go To Special dialog box, check the Bla ...

  7. 文档相关命令-cat命令查看一个文件

    用于查看一个文件的内容并将其显示在屏幕上 cat 后直接加上文件名 -n  表示显示行号 cat -n dirb/filee -A 显示所有内容包括特殊字符 cat -A dirb/filee

  8. linux进程控制函数详解

    进程控制 fork函数 创建一个子进程. pid_t fork(void); 失败返回-1:成功返回:① 父进程返回子进程的ID(非负) ②子进程返回 0 pid_t类型表示进程ID,但为了表示-1, ...

  9. Aspose.Words 操作指北

    前言:   这段时间因为业务需求,需要做一个word 导出,表单式的那种,因为之前也做过NPOI 操作 Excel 这类型的问题,所以总以为word 导出应该挺简单的,一直没有研究这块,汗颜,天真大意 ...

  10. HTML、CSS规范

    作为一名前端开发者,至少要对HTML.CSS规范有个了解,然后尝试在项目是使用,以便别人阅读你代码的时候,也相对轻松点. HTML.CSS规范,参见:编码规范 by @mdo JavaScript 参 ...