普通广播(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"));
} }

程序效果

最新文章

  1. php基础语法-函数等
  2. 词频统计(WEB版)
  3. GCC 命令行详解 -L 指定库的路径 -l 指定需连接的库名(转载)
  4. parentViewController
  5. 浅谈angular中的promise
  6. springboot+CXF开发webservice对外提供接口(转)
  7. python趣味——与MS系列编译器一样强大的Unicode变量名支持
  8. delete web server(nginx)
  9. BZOJ.4320.[ShangHai2006]Homework(根号分治 分块)
  10. Fiddler&#160;使用fiddler发送捕获的请求及模拟服务器返回
  11. 【Vue】【Router】手动跳转用 this.$router.push() 时 $router 未定义的问题
  12. Python案例
  13. QQ去除未读状态的动画
  14. iOS开发-iOS7禁用手势返回
  15. (转)权威支持: 选择正确的 WebSphere 诊断工具
  16. jacky自问自答-数据库
  17. linux C 程序内存布局
  18. css 各种常见布局整理
  19. Linux系统启动流程与系统目录
  20. django笔记-model

热门文章

  1. How To: set udev rule for setting the disk permission on ASM disks when using multipath on Linux 6.x
  2. LOJ——#6277. 数列分块入门 1
  3. node.js 中的package.json文件怎么创建?
  4. 【Codeforces Global Round 1 A】Parity
  5. (42)Spring Boot多数据源【从零开始学Spring Boot】
  6. nyoj 307
  7. [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心
  8. 记一个简单的webpack.config.js
  9. Python3-paramiko-SFTP上传文件夹到多台远程服务器
  10. GDB 调试 PHP文件