无序广播:一条广播发送出去,多个接收者接收没有顺序。
有序广播:广播接收者可以设置优先级,优先级高的先收到广播。
有序广播可以设置优先级。
mainActivity.java
package com.sxt.day07_06; import android.os.Bundle; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.util.Log; import android.view.Menu; public class MainActivity extends Activity { ThirdReceiver mReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); registgerReceiver(); Intent intent=new Intent("com.sxt.day07_06.order_broadcast"); intent.putExtra("key", "hello java"); sendOrderedBroadcast(intent, null);//发送有序广播 } //注册第三级别的广播接收者,第一第二级别的广播接收者在系统说明文件中注册, private void registgerReceiver() { mReceiver=new ThirdReceiver(); IntentFilter filter=new IntentFilter("com.sxt.day07_06.order_broadcast");//com.sxt.day07_06.order_broadcast广播的字符串 filter.setPriority(600);//设置优先级 registerReceiver(mReceiver, filter); } //java定义的第三级别的广播接收这 class ThirdReceiver extends BroadcastReceiver{ public void onReceive(Context context, android.content.Intent intent) { Log.i("main","ThirdReceiver.onReceive()"); }; } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mReceiver);//销毁的时候取消注册的广播接收者,释放资源。 } }
第一个广播接收者:
package com.sxt.day07_06; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class FirstReceiver extends BroadcastReceiver { @Override //优先级第一的广播接收者 public void onReceive(Context context, Intent intent) { String value=intent.getStringExtra("key");//mainActivity的字符串 Log.i("main","FirstReceiver.onReceive():"+value); setResultData("来源于FirstReceiver的文本");//向第二级别的广播接收者发送字符串, } }
第二个广播接收者:
package com.sxt.day07_06; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class SecondReceiver extends BroadcastReceiver { @Override //优先级第二的广播接收者 public void onReceive(Context context, Intent intent) { String value=intent.getStringExtra("key");//mainActivity的字符串 String text=getResultData();//第一级别广播接收者发过来的数据 Log.i("main","SecondReceiver.onReceive(),intent中携带的文本:"+value+","+text); if(isOrderedBroadcast()){//如果收到的广播是有序广播 abortBroadcast();//终止广播 } } }
系统描述文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sxt.day07_06" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.sxt.day07_06.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.sxt.day07_06.FirstReceiver"> 注册广播接收者 <intent-filter android:priority="800"> <action android:name="com.sxt.day07_06.order_broadcast"/> </intent-filter> </receiver> <receiver android:name="com.sxt.day07_06.SecondReceiver"> <intent-filter android:priority="700"> <action android:name="com.sxt.day07_06.order_broadcast"/> </intent-filter> </receiver> </application> </manifest>
本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/4893012.html,如需转载请自行联系原作者