android-BroadcastReceiver 发送有序广播
来源:互联网 发布:centos制作u盘启动 编辑:程序博客网 时间:2024/05/16 01:42
普通广播(Normal Broadcast):
一,可以在同一时刻被所有接收者收到,消息传递效率比较高,但缺点是接收者不能将结果传递给下一个接收者,且无法终止Broadcast Intent的传播
二,发送广播的方法:sendBroadcast()
有序广播(Ordered Broadcast):
一,优缺点
优点:1,按优先级的不同,优先Receiver可对数据进行处理,并传给下一个Receiver
2,通过abortBroadcast可终止广播的传播
缺点:效率低
二,发送广播的方法:sendOrderedBroadcast()
三,优先接收到Broadcast的Receiver可通过setResultExtras(Bundle)方法将处理结果存入Broadcast中,
下一个Receiver 通过 Bundle bundle=getResultExtras(true)方法获取上一个 Receiver传来的数据
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.song" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <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,优先级的范围-1000~1000 --> </activity> <receiver android:name=".MyReceiver1"> <intent-filter android:priority="200"> <action android:name="com.song.123"/> </intent-filter> </receiver> <receiver android:name=".MyReceiver2"> <intent-filter android:priority="1000"> <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); } }); } }
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")); } }
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(); } }
程序效果
0 0
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver 发送有序广播
- android-BroadcastReceiver有序广播
- Android BroadcastReceiver实例Demo(有序广播的发送)
- Android 四大组件之BroadcastReceiver(发送有序无序广播)
- 发送有序广播——BroadcastReceiver
- Android 发送有序广播
- Android-发送有序广播
- Android 发送有序广播
- Android的Service与BroadcastReceiver之四 BroadcastReceiver 简介、开发与配置;发送广播、有序广播;使用BroadcastReceiver接受系统广播
- 初学Android,BroadcastReceiver之有序广播(七十一)
- Android BroadcastReceiver之自定义无序、有序广播
- Android自定义发送有序广播
- Android-广播接收者:发送有序广播
- Android--广播接收者(发送有序广播)
- Linux下" >/dev/null 2>&1 "相关知识说明
- Android获取联系人
- 基于代数距离的椭圆拟合
- Spring_22_基于配置文件的方式来配置 AOP
- U盘制作Apple系统安装盘
- android-BroadcastReceiver 发送有序广播
- 找老乡 (sdut oj)
- 数据结构实验之二叉树的建立与遍历
- CF699A
- 关于Jadepool3.0的使用
- post请求https接口
- SPOJ 694&&705
- ajax提交的javascript代码
- 如何快速转载CSDN中的博客