BroadcastReceiver
在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制。而BroadcastReceiver是对发送出来的Broadcast进行过滤接受并响应的一类组件。
下面将详细的阐述如何发送Broadcast和使用BroadcastReceiver过滤接收的过程:
首先在需要发送信息的地方,把要发送的信息和用于过滤的信息(如Action、Category)装入一个Intent对象,然后通过调用sendOrderBroadcast()或sendStickyBroadcast()方法,把Intent对象以广播方式发送出去。
当Intent发送以后,所有已经注册的BroadcastReceiver会检查注册时的IntentFilter是否与发送的Intent相匹配,若匹配则就会调用BroadcastReceiver的onReceive()方法。所以当我们定义一个BroadcastReceiver的时候,都需要实现onReceive()方法。
注册BroadcastReceiver有两种方式:
静态注册:在AndroidManifest.xml中用标签生命注册,并在标签内用标签设置过滤器。
<receiverandroid:name="myRecevice">
<intent-filter>
<actionandroid:name="com.dragon.net"></action>
</receiver>
动态注册:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(String);
一般:在onStart中注册,onStop中取消unregisterReceiver
指定广播目标Action:Intent intent = new Intent(actionString);
并且可通过Intent携带消息 :intent.putExtra("msg", "hi,我通过广播发送消息了");
发送广播消息:Context.sendBroadcast(intent
其中在动态注册中可将BroadcastReceiver的继承类进行封装,添加构造函数和BroadcastReceiver注册
import android.app.Notification;import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
public class BroadcastReceiverHelper extends BroadcastReceiver {
}
然后再Activity中声明BroadcastReceiver的扩展对象,在onStart中注册,onStop中卸载
BroadcastReceiverHelper
@Override