普通广播(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 发送有序广播的更多相关文章

  1. 发送有序广播Ordered Broadcast

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

  2. android#boardcast#发送自定义广播

    广播主要分为两种类型,标准广播和有序广播,通过实践的方式来看下这两种广播具体的区别. 一.发送标准广播 在发送广播之前,我们还是需要先定义一个广播接收器来准备接收此广播才行,不然发出去也是白发.因此新 ...

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

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

  4. Android(java)学习笔记179:BroadcastReceiver之 有序广播和无序广播(BroadcastReceiver优先级)

    之前我们在Android(java)学习笔记178中自定义的广播是无序广播,下面我们要了解一下有序广播:   1.   我们首先了解一下有序广播和无序广播区别和联系? (1) 有序广播> 接受者 ...

  5. Android(java)学习笔记122:BroadcastReceiver之 有序广播和无序广播(BroadcastReceiver优先级)

    之前我们在Android(java)学习笔记178中自定义的广播是无序广播,下面我们要了解一下有序广播: 1. 我们首先了解一下有序广播和无序广播区别和联系? (1)有序广播> 接受者有优先级, ...

  6. BroadcastReceiver之有序广播

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

  7. 查看Android 系统发送的广播

    命令行输入如下命令 adb shell dumpsys |grep BroadcastRecord

  8. android BroadcastReceiver

    AndroidManifast.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...

  9. Android——BroadcastReceiver

    注释:一般广播不会被阻断,有序广播则会被阻断 注释:这是用动态注册的广播,必须要解绑 xml <?xml version="1.0" encoding="utf-8 ...

随机推荐

  1. Configuring SSL on Enterprise Manager and the SLB (Release 12.1.0.2 and later)

    From: http://docs.oracle.com/html/E24089_42/ha_setup.htm#sthref833 If the SLB is configured to use T ...

  2. 65.dynamic mapping

    主要知识点: 理解dynamic mapping 定制dynamic mapping 更改default dynamic mapping     一.理解dynamic mapping 1.基本概念 ...

  3. 从零开始的 webpack4 + vue2.x

    新建文件夹 webpack-vue 安装依赖 yarn init //初始化package.json yarn add webpack webpack-cli //添加webpack.webpack- ...

  4. 关于新世界的大门(新博客地址:BBBob.cf)

    更新:BBBob.cf 这个域名已经不用了(但是依旧可以访问),永久域名改为了BBBob.win 新博客地址为BBBob.cf,以后的博客都会在新博客更新,当然在新博客上我也会写得更用心些,不再像这里 ...

  5. 【ACM】hdu_2004_成绩转换_201307261516

    成绩转换Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  6. http-runtime属性

    配置httpRuntime也可以让FileUpload上传更大的文件,不过设置太大了会因用户将大量文件传递到该服务器而导致的拒绝服务攻击(属性有说明) 属性 属性 选项 说明 appRequestQu ...

  7. [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心

    Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...

  8. [ javascript ] getElementsByClassName与className和getAttribute!

    对于javascript中的getElementsByClassName 在IE 6/7/8 不支持问题. 那么须要模拟出getElementsByClassName  须要採用className属性 ...

  9. [Tools] Using colours in a NodeJS terminal - make your output pop

    Use can use colour and styles to make it easy to spot errors and group common functionality into blo ...

  10. Android Studio第一次启动的Fetching android sdk component information的问题

    1)进入刚安装的Android Studio文件夹下的bin文件夹.找到idea.properties文件,用文本编辑器打开. 2)在idea.properties文件末尾加入一行: disable. ...