应用程序中Broadcast Receiver的两种注册方法及比较
分类: android
2012-03-09 18:52
1165人阅读
评论(2)
收藏
举报
service
android
filter
扩展
工作
当系统或用户应用程序发送了某广播之后,符合条件的广播接收者都将收到该条广播。发送广播一般是通过sendBroadcast(Intent intent)(或者sendStickyBroadcast)或者sendOrderedBroadcast())方法来实现的(这两种发送方式分别对应了两种不同的广播,一种是普通的Broadcast,一种是有序的Broadcast,),其中intent为要广播的Intent,所谓符合条件就是广播接收者只接收接收者的IntentFilter过滤之后的Intent,所以为了使广播接收者能够接收某种intent,则必须要注册这个广播接收者。
这里为理解方便,简单介绍下Broadcast Receiver。Broadcast Receiver是Android的四大组件之一(还有Activity、Service、Content Provider),作用就是用以监听系统或用户程序broadcast的Intent,它本质上是系统的一种全局监听器(与onXxxxListener相似但不同),只要存在与之匹配的Intent被广播出来,BroadcastReceiver就会被激活,所以BroadcastReceiver与具有完整生命周期的Activity或Service不同(广播接收者只有一个生命周期回调函数onReceive)。BroadcastReceiver的工作流程是这样的:系统程序或用户程序广播了某Intent之后,就会被与该Intent匹配的广播就收者(这个广播接收者可以是用户自己通过扩展BroadcastReceiver得到的Receiver)所接收,接着就会执行onReceive(Context context, Intent intent)方法中的代码,在这里可以完成自己要实现的功能。
所以,为使Receiver与广播的某Intent相匹配,则需要注册该Receiver,注册一个广播接收者Receiver方法有两种,可以在AndroidManifest.xml中通过<receiver>标签注册,比如下面的注册方式,表示收到一条短信,系统发送一条广播,这条广播将被自己扩展的广播接收者所接收:
[java]
view plain
copy
print
?
- <receiver android:name=“包名.自己扩展的广播接收者名”>
- <intent-filter>
- <action android:name=“android.provider.Telephony.SMS_RECEIVED”/>
- </intent-filter>
- </receiver>
<receiver android:name="包名.自己扩展的广播接收者名">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
但是使用这种注册方式注册了之后,即使程序已经关闭或者没有开启,都将接收到匹配的广播。还有一种更灵活的注册方式是在代码中注册,这种方式可以完成类似这样的场景,满足某条件时注册广播接收者来接收广播,在另一种条件下时便解除注册广播接收者不再接收广播。具体就是通过两个方法来实现注册和解除注册的:
registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
和
unRegisterReceiver(BroadcastReceiver)
。注册
Receiver
方式如下:
[java]
view plain
copy
print
?
- IntentFilter filter = new IntentFilter(“android.provider.Telephony.SMS_RECEIVED”);
- MyReceiver receiver = new MyReceiver();
- registerReceiver(receiver, filter);
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
MyReceiver receiver = new MyReceiver();
registerReceiver(receiver, filter);
这里需要说明的是,除了用于过滤广播的
IntentFilter
可以在代码中动态创建外,其他组件的
IntentFilter
必须在
AndroidManifest.xml
中注册。
PS:前面我们说BroadcastReceiver本质上一种系统级的全局监听器,因此BroadcastReceiver就为在不同的组件之间进行通信提供了一种新思路。比如程序有一个Activity、一个Service,而且该Service是通过startService()方法(Service还有种启动方式是通过bindService()方法启动)启动起来的,一般情况下这个Activity与通过startService()方法启动的Service之间是无法通信的,但借助BroadcastReceiver的帮助,程序就可实现两者之间的通信(其实本质上还是通过Intent实现的组件间的通信)。关于通过Intent来实现组件间的通信,可以参考我的下一篇博文哈。
第一种在代码中使用registerReceiver注册
第二种在清单文件androidmanifest中注册(常驻Receiver)
private TextView textview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textview = (TextView)this.findViewById(R.id.textview);
//注册自定义BroadcastReceiver(1)
this.registerReceiver(mBroadcastReceiver, new IntentFilter(“com.xxx.action.MYRECEIVER”));
Intent intent = new Intent(“com.xxx.action.MYRECEIVER”);
this.sendBroadcast(intent);
}
//注册自定义BroadcastReceiver(2)
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(“com.xxx.action.MYRECEIVER”)){
textview.setText(“com.xxx.action.MYRECEIVER”);
}
}
};
在onCreate中注册自定义(1)mBroadcastReceiver,并用Intent封装消息,最后sendBroadcast发送广播,然后程序进入到BroadcastReceiver 的定义(2)中,在重载的onReceicer这里会处理广播
第二种:
Androidmanifest文件中添加:
<receiver android:name=”.InfoReceiver”>
<intent-filter>
<action android:name=”com.mobimtech.action.BROADCAST”/>
</intent-filter>
</receiver>
编写继承broadcast类:
package com.mobimtech.message;
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.util.Log;
public class InfoReceiver extends BroadcastReceiver {
private static final int REQUEST=1;
private static final int REPLY=2;
private static final int MESSAGE=3;
private static final int REPLY_MESSAGE=4;
private Context mContext;
private String mValue;
private String mTemp[];
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
mContext=context;
mValue=intent.getStringExtra(“key”);
Log.e(“receiver”, “…………”+mValue);
mTemp=mValue.split(“#”);
show(mTemp[0]);
// 请求指令格式:指令代号+发送方+接收方+附加消息
// 恢复指令消息:指令代号+发送方+接收方+yes/no
}
//显示消息
public void show(String id){
if(id.equals(“request”)){
NotificationManager manager=(NotificationManager)mContext.getSystemService
(android.content.Context.NOTIFICATION_SERVICE);
Intent intent=new Intent();
intent.setClass(mContext,Request.class);
intent.putExtra(“key”,mValue);
PendingIntent appIntent=PendingIntent.getActivity(mContext, 0, intent,0);
Notification notification=new Notification();
notification.icon=R.drawable.notification;
notification.tickerText=”新消息”;
notification.setLatestEventInfo(mContext,mTemp[1],”请求加你为好友”,appIntent);
manager.notify(REQUEST, notification);
Log.e(“key”,”……………”);
}else if(id.equals(“reply”)){
NotificationManager manager=(NotificationManager)mContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
Intent intent=new Intent();
intent.setClass(mContext, Reply.class);
intent.putExtra(“key”,mValue);
PendingIntent appIntent=PendingIntent.getActivity(mContext, 0, intent,0);
Notification notification=new Notification();
notification.icon=R.drawable.notification;
notification.tickerText=”新消息”;
if(mTemp[3].equals(“refuse”)){
notification.setLatestEventInfo(mContext,mTemp[1],”拒绝添加你为好友”,appIntent);
}else{
notification.setLatestEventInfo(mContext,mTemp[1],”同意添加你为好友”,appIntent);
}
manager.notify(REPLY, notification);
}else if(id.equals(“message”)){
NotificationManager manager=(NotificationManager)mContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
Intent intent=new Intent();
intent.setClass(mContext, ReplyMsg.class);
intent.putExtra(“key”,mValue);
PendingIntent appIntent=PendingIntent.getActivity(mContext, 0, intent,0);
Notification notification=new Notification();
notification.icon=R.drawable.notification;
notification.tickerText=”新消息”;
notification.setLatestEventInfo(mContext,mTemp[1],”给你发了短信”,appIntent);
manager.notify(MESSAGE, notification);
Log.e(“Notification”,”Notification”);
}else if(id.equals(“replymessage”)){
NotificationManager manager=(NotificationManager)mContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
Intent intent=new Intent();
intent.setClass(mContext, ReplyMsg.class);
intent.putExtra(“key”,mValue);
PendingIntent appIntent=PendingIntent.getActivity(mContext, 0, intent,0);
Notification notification=new Notification();
notification.icon=R.drawable.notification;
notification.tickerText=”新消息”;
notification.setLatestEventInfo(mContext,mTemp[1],”回复你发了短信”,appIntent);
manager.notify(REPLY_MESSAGE, notification);
}
}
}
代码使用广播:
Intent intent=new Intent(ACTION);
intent.putExtra(“key”, value);
sendBroadcast(intent);