1.自定义广播
①编写一个类继承BroadcastReciever,重写onRecieve方法。
②在AndroidManifest配置文件中的application结点下配置reciever节点:
<receiver android:name=".+**写继承BroadcastRecieve的类**" >
<intent-filter android:priority="1000" > //优先级(优先级高的先执行)
<action android:name="com.wq.xxx" />//自己定义的广播动作
</intent-filter>
</receiver>
2.发送广播事件
①创建新的工程在类中写如下关键代码
Intent intent=new Intent();
intent.setAction("com.wq.xxx");//和自定义广播中的<action android:name="com.wq.xxx" />一致
//发送无序广播
//无序广播的特点:所有接收者均可接收到
//sendBroadcast(intent);
//发送有序广播
//有序广播的特点:接收者按优先级接收,并且高优先级的广播接受者可以拦截低优先级的接受者
//sendOrderedBroadcast(intent, null);
sendOrderedBroadcast(intent, null, new FinalBroadCast(), null, 0, null, null);
FinalBroadCast也是继承BroadcastRecieve的一个类,
②也可终止广播,则比该广播优先级低的接收不到
//终止广播
abortBroadcast();
但是使用sendOrderedBroadcast(intent, null, new FinalBroadCast(), null, 0, null, null);方法,FinalBroadCast能接收到。