BroadcastReceiver使用方式

从行为上广播类型有两种:
无序广播(Normal broadcasts):无序且并行执行广播
有序广播(Ordered broadcasts):有序地一个接一个执行广播

当通过意图发送广播时,无论BroadcastReceiver定义在当前应用还是其他应用中,只要BroadcastReceiver对此意图感兴趣(如:匹配Action),就会得到执行。

有序广播和无序广播默认都是运行在UI线程中的,当BroadcastReceiver在10秒内没有执行完毕,Android会认为该程序无响应,所以在BroadcastReceiver里不能做一些比较耗时的操作,否则会弹出ANR(Application No Response)的对话框。如果需要完成一项比较耗时的工作,应该通过Intent发送给Service,由Service来完成,而不是使用子线程的方法来解决,因为BroadcastReceiver的生命周期很短(在onReceive()执行后BroadcastReceiver的实例就会被销毁),子线程可能还没有结束BroadcastReceiver就先结束了。如果BroadcastReceiver结束了,它的宿主进程还在运行,那么子线程还会继续执行。但宿主进程此时很容易在系统需要内在时被优先杀死。因为它属于空进程(没有任何活动组件的进程)。


下面分别讲解这两种广播


无序广播:
示例中FirstBroadcastReceiver和SecondBroadcastReceiver在同一个应用中,ThirdBroadcastReceiver在另一个应用中。

他们的执行顺序是不可预知的。

应用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"));
	}
}

AndroidManifest.xml中配置BroadcastReceiver

<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>

在一个名为BroadcastReceiverDemo的Activity中定义一个Button,点击事件如下

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"));
	}
}

AndroidManifest.xml中配置BroadcastReceiver

<receiver android:name=".ThirdBroadcastReceiver">
	<intent-filter>
		<action android:name="com.improve.broadcast"/>
	</intent-filter>
</receiver>

运行结果




有序广播:
通过android:priority="N"设置广播执行顺序(N的取值-1000到1000),优先级高的广播先执行。(这里先执行SecondBroadcastReceiver再执行FirstBroadcastReceiver)
在前一个执行的广播中设置结果值,传给下一个广播。也可以通过 BroadcastReceiver.abortBroadcast()终止广播,后面的广播接收器不会得到执行。

定义两个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);
	}
}

AndroidManifest.xml中配置BroadcastReceiver

<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>

在一个名为BroadcastReceiverDemo的Activity中定义一个Button,点击事件如下

new OnClickListener() {
	@Override
	public void onClick(View button) {
		//发送广播
		Intent intent = new Intent("com.improve.broadcast");
		intent.putExtra("msg", "BroadcastReceiverDemo");
		sendOrderedBroadcast(intent, null);
	}
}

运行结果

你可能感兴趣的:(android,service,application,Class,action,button)