Android之Broadcast Receiver的两种注册方式

来源:互联网 发布:淘宝历史订单保留多久 编辑:程序博客网 时间:2024/04/28 20:02

Broadcast Receiver是Android的四大组件之一,其本质是系统的全局监听器,作用是监听系统或应用程序发出的广播的Intent,只要有与之匹配的Intent被广播出来,Broadcast Receiver就会被激活,激活后执行onReceiver(Context context, Intent intent)方法中的代码,实现应用想要的逻辑。

若使Broadcast Receiver能与广播出来的Intent匹配,则需要注册该Receiver,有两种注册方式。

方式一,静态注册,通过AndroidManifest.xml中的<receiver>标签注册;此种方式注册的Receiver必须继承BroadcastReceiver,静态注册也需要intent-filter,参考代码如下:

public class PingReceiver extends BroadcastReceiver{private static final String TAG = PingReceiver.class.getSimpleName();@Overridepublic void onReceive(Context context, Intent intent) {LogUtil.i(TAG, "receiver action : " + intent.getAction());if (intent.getAction().equals("COM.ALARM.ACTION.WEAKUPNOWTASK")) {Toast.makeText(context, "定时闹钟", Toast.LENGTH_LONG).show();} else if(intent.getAction().equals("COM.ALARM.ACTION.REPEATETASK")){Toast.makeText(context, "重复闹钟", Toast.LENGTH_LONG).show();}}}

<receiver android:name=".service.util.PingReceiver">    <intent-filter>        <action android:name="COM.ALARM.ACTION.WEAKUPNOWTASK"/>        <action android:name="COM.ALARM.ACTION.REPEATETASK"/>    </intent-filter></receiver>    Intent intent =new Intent(mCtx, PingReceiver.class);    intent.setAction("COM.ALARM.ACTION.WEAKUPNOWTASK");    mCtx.sendBroadcast(intent);

方式二,动态注册,在程序中通过Context.registerReceiver来注册,取消注册则用mCtx.unregisterReceiver,注册的广播接收器相当于一个匿名类,参考代码如下:

//注册广播接收器,并添加IntentFilter关键字。public void init() {IntentFilter filter = new IntentFilter();filter.addAction("COM.ALARM.ACTION.REPEATETASK");filter.addAction("COM.ALARM.ACTION.WEAKUPNOWTASK");mCtx.registerReceiver(mReceiver, filter);}//广播接收器private BroadcastReceiver mReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {LogUtil.i(TAG, "receiver action : " + intent.getAction());if (intent.getAction().equals("COM.ALARM.ACTION.WEAKUPNOWTASK")) {Toast.makeText(context, "short alarm", Toast.LENGTH_LONG).show();} else if(intent.getAction().equals("COM.ALARM.ACTION.REPEATETASK")){Toast.makeText(context, "repeating alarm", Toast.LENGTH_LONG).show();}}};//发送广播Intent intent = new Intent("COM.ALARM.ACTION.WEAKUPNOWTASK");mCtx.sendBroadcast(intent);//取消注册mCtx.unregisterReceiver(mReceiver);


广播操作的流程总结:
(1)注册广播
静态注册方式和动态注册方式
(2)发送广播
通过如下方式:
mCtx.sendBroadcast(intent);
mCtx.sendOrderedBroadcast(intent, receiverPermission);
利用AlarmManager的PendingIntent sender = PendingIntent.getBroadcast(mCtx, 0, intent, 0);也可以实现发送广播,可以参考接下来的文章:利用AlarmManager实现定时任务。
(3) 接收广播
发送的广播被接收器监听到后,会调用它的onReceiver()方法,可以通过判断intent的不同action来区分不同的广播事件。注意onReceiver中代码的执行时间不要超过5秒钟。
(4)取消注册
分为两种情况:
动态注册时,调用unregisterReceiver即可,为了减轻系统负担,好的编码习惯是unregisterReceiver与registerReceiver最好配对编写。
当静态注册时,是否需要通过代码注销注册呢,情况就有些复杂了,找了一些资料发现android似乎并没有提供相关线索,最后在网上找到一个帖子:Android的源代码中有这样的注释:If this BroadcastReceiver was launched through a <receiver> tag, then the object is no longer alive after returning from this function. This means you should not perform any operations that return a result to you asynchronously. 这段文字告诉我们:如果通过<receiver>标签来注册的BroadcastReceiver,那么该对象的实例在onReceive被调用之后就会在任意时间内被销毁。也就是说,我们并不需要担心静态注册的BroadcastReceiver的销毁问题。具体可参考:Michael在OPhoneSDN上提出这个疑问。

欢迎各位朋友一起讨论,如需转载,请注明出处,谢谢。

原创粉丝点击