在android中两种BroadcastReceiver广播组件的使用情况:
自定义广播 MyBroadcastReceiver extends BroadcastReceiver 重写onReceive方法
1.通过在AndroidManifest,xml中配置
如:
<receiver android:name="com.hanwj.MyBroadcastReceiver">
<intent-filter>
<action android:name="com.hanwj.RECEIVER"></action>------------------------------------(1)-------------------------------------------
</intent-filter>
</receiver>
2.通过程序创建BroadcastReceiver
IntentFilter filter=new IntentFilter();--相当于配置文件中<intent-filter>标签
filter.addAction("com.hanwj.RECEIVER");--添加频段-------------------------------------------------(2)-----------------------------------
registerReceiver(receiverBroad,filter);---注册广播 receiverBroad 为MyBroadcastReceiver 实例 ,
unregisterReceiver(receiverBroad);---取消广播
//发送广播
Intent intent=new Intent();
intent.setAction("com.hanwj,RECEIVER");---------------------------------------------------------------(3)--------------------------------------
sendBroadcast(intent);
注意:不管是用配置文件注册还是使用程序创建广播,发送广播时----保持频率一致---否则无法接收到广播内容
使用配置:(1),(3) 一致
使用程序:(2),(3)一致