1.两种方法发送广播:
(1) Context.sendBroadcast( )
所有满足条件的BroadcastReceiver都会执行onReceive( )方法处理响应
(2) Context.sendOrderedBroadcast( )发送有序广播,根据BroadcastReceiver 注册时IntentFilter优先级顺序执行onReceive( )方法
优先级在<intent_filter>的Android:priorty中声明,也可以通过代码IntentFilter.setPriority()设置,数越大,优先级别越高。
2.Broadcast Receiver需要在AndroidManifest.xml中注册,在内设置过滤器
定义广播
定义广播接收器
new java class xxx extends BroadcastReceiver 重写onReceive(){ 接收到广播后的动作}
注册广播接收器
<receiver android:name=".xxx"
android:exported="true">
<intent-filter>
<action android:name="自定义"></action>
</intent-filter>
</receiver>
MainActivity中:
对广播建立监听,重写onClick(){
Intent intent = new Intent();
intent.setComponent(new ComponentName("pkg,"cls"));
intent.setAction("自定义");
intent.putExtra("name","value");
sendBroadcast(intent);
}