当通过意图发送广播时,无论BroadcastReceiver定义在当前应用还是其他应用中,只要BroadcastReceiver对此意图感兴趣(如:匹配Action),就会得到执行。
有序广播和无序广播默认都是运行在UI线程中的,当BroadcastReceiver在10秒内没有执行完毕,Android会认为该程序无响应,所以在BroadcastReceiver里不能做一些比较耗时的操作,否则会弹出ANR(Application No Response)的对话框。如果需要完成一项比较耗时的工作,应该通过Intent发送给Service,由Service来完成,而不是使用子线程的方法来解决,因为BroadcastReceiver的生命周期很短(在onReceive()执行后BroadcastReceiver的实例就会被销毁),子线程可能还没有结束BroadcastReceiver就先结束了。如果BroadcastReceiver结束了,它的宿主进程还在运行,那么子线程还会继续执行。但宿主进程此时很容易在系统需要内在时被优先杀死。因为它属于空进程(没有任何活动组件的进程)。
他们的执行顺序是不可预知的。
应用A中代码:
定义两个BroadcastReceiver
public class FirstBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i("CDH", "FirstBroadcastReceiver msg:"+intent.getStringExtra("msg")); } } public class SecondBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i("CDH", "SecondBroadcastReceiver msg:"+intent.getStringExtra("msg")); } }
<receiver android:name=".FirstBroadcastReceiver"> <intent-filter> <action android:name="com.improve.broadcast"/> </intent-filter> </receiver> <receiver android:name=".SecondBroadcastReceiver"> <intent-filter> <action android:name="com.improve.broadcast"/> </intent-filter> </receiver>
new OnClickListener() { @Override public void onClick(View button) { //发送广播 Intent intent = new Intent("com.improve.broadcast"); intent.putExtra("msg", "I am BroadcastReceiverDemo Activity"); sendBroadcast(intent); } }
应用B中代码:
public class ThirdBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i("CDH", "ThirdBroadcastReceiver msg:"+intent.getStringExtra("msg")); } }
<receiver android:name=".ThirdBroadcastReceiver"> <intent-filter> <action android:name="com.improve.broadcast"/> </intent-filter> </receiver>
运行结果
定义两个BroadcastReceiver
public class FirstBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i("CDH", "FirstBroadcastReceiver msg:"+intent.getStringExtra("msg")); Bundle mBundle = this.getResultExtras(false);//true:前一个广播没有结果时创建新的Bundle;false:不创建Bundle if (mBundle == null) { Log.i("CDH", "没有获取到前一个运行的广播返回的数据"); } else { Log.i("CDH", "前一个运行的广播返回的数据为:" + mBundle.getString("msg")); } } } public class SecondBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i("CDH", "SecondBroadcastReceiver msg:"+intent.getStringExtra("msg")); Bundle bundle = new Bundle(); bundle.putString("msg", "SecondBroadcastReceiver"); setResultExtras(bundle); } }
<receiver android:name=".FirstBroadcastReceiver"> <intent-filter android:priority="1"> <action android:name="com.improve.broadcast"/> </intent-filter> </receiver> <receiver android:name=".SecondBroadcastReceiver"> <intent-filter android:priority="2"> <action android:name="com.improve.broadcast"/> </intent-filter> </receiver>
new OnClickListener() { @Override public void onClick(View button) { //发送广播 Intent intent = new Intent("com.improve.broadcast"); intent.putExtra("msg", "BroadcastReceiverDemo"); sendOrderedBroadcast(intent, null); } }