一 、注册BroadcastReceiver的两种方法:
1 在AndroidManifest.xml中配置,如:
<receiver android:name="MyReceiver">
<intent-filter>
<actionandroid:name="MyReceiverAction" />
</intent-filter>
</receiver>
在这种方式下,即使注册广播接收器的程序关闭,接收器仍然会接收到广播,除非从模拟器或手机中卸载程序或注销接收器,否则无法阻止接收器接收广播。
2 在代码中指定:
如果想在适当的时候注册广播接收器,在使用完后将其注销,就需要直接使用java代码来完成这个功能。
MyReceiver receiver = new MyReceiver();//MyReceiver是自己定义的继承
BroadCastReceive 的类
//IntentFilter filter = new IntentFilter();
//filter.addAction("MyReceiverAction");
IntentFilter filter = new IntentFilter("MyReceiverAction"); registerReceiver(receiver,filter);
这种方式下,即使没有注销广播接收器,当应用程序退出时也会自动注销。
注销广播接收器:
unregisterReceiver(receiver);
二、发送广播消息:
Intent intent = new Intent();
intent.setAction("MyReceiverAction");
intent.putExtra("msg", "HelloWorld!");
//发送普通广播
//sendBroadcast(intent);
//放送有序广播
sendOrderedBroadcast(intent,null);
三、有序广播
Broadcast(广播)分为两种:
1 Normal Broadcast (普通广播):NormalBroadcast 是完全异步的,可以在同一个时刻(逻辑上)可以被所有的接收者收到,消息的传递效率比较高。缺点是接收者不能将处理结果传递给下一个接收者,并且不能阻止BroadcastIntent 的传播。
2 Ordered Broadcast (有序广播):OrderedBroadcast 的接收者按照广播的优先级依次接收广播,优先级可以在<intent-filter…/>的android:priority属性中配置,也可以在代码中调用IntentFilter对象的setPriority()设置,并且数值越大优先级越高。
sendBroadcast()发送普通广播;
sendOrderedBroadcast()放送有序广播,广播接收器按照优先级的高低调用,优先接收到广播的接收者可以传递数据给下一个接收者,也可以调用BroadcastReceiver的abortBroadcast()阻止广播传递给其它接收者。
不管是发送普通广播还是放送有序广播,如果设置了优先级,则按照优先级的高低调用接收器,如果不设置优先级或者优先级相同,则对于同一个应用程序中的广播接收器会按照在AndroidManifest.xml中定义的顺序调用。
下面注册了两个接收器MyReceiver和Myreceiver1:
<receiver android:name="MyReceiver" >
<intent-filterandroid:priority="10">
<actionandroid:name="MyReceiverAction" />
</intent-filter>
</receiver>
<receiverandroid:name="MyReceiver1" >
<intent-filterandroid:priority="1">
<actionandroid:name="MyReceiverAction" />
</intent-filter>
</receiver>
public class MyReceiver extends BroadcastReceiver{
public MyReceiver(){
System.out.println("MyReceiver");
}
@Override
public void onReceive(Contextcontext, Intent intent) {
System.out.println("onReceive");
Toast.makeText(context,"MyReceiver接收Intent的Action为:"
+intent.getAction() +"\n消息内容为:"
+intent.getStringExtra("msg"),Toast.LENGTH_LONG).show();
//创建一个Bundle对象并存入数据
Bundle bundle = new Bundle();
bundle.putString("first","第一个BroadcastReceiver存入的消息");
setResultExtras(bundle);
//取消Broadcast的继续传播
//abortBroadcast();
}
}
public class MyReceiver1 extends BroadcastReceiver{
public MyReceiver1(){
System.out.println("MyReceiver1");
}
@Override
public void onReceive(Contextcontext, Intent intent) {
System.out.println("onReceive");
Bundle bundle = getResultExtras(true);
String first =bundle.getString("first");
Toast.makeText(context,"MyReceiver1接收到的Intent的Action为:"
+intent.getAction() +"\n消息内容为:"
+intent.getStringExtra("msg") +"\n" + first,Toast.LENGTH_LONG).show();
}
放送广播:
Intent intent = new Intent();
intent.setAction("MyReceiverAction");
intent.putExtra("msg", "HelloWorld!");
//发送普通广播
//sendBroadcast(intent);
//放送有序广播
sendOrderedBroadcast(intent,null);
放送有序广播后,优先级高的MyReceiver先接收到广播,并传递数据给MyReceiver1,
如果在MyReceiver的onReceiver()中调用abortBroadcast(),则MyReceiver1不会接收到广播。