Android BroadcastReceiver [转]
BroadCastReceiver 简介
BroadCastReceiver 源码位于: framework/base/core/java/android.content.BroadcastReceiver.java
广播接收者( BroadcastReceiver )用于接收广播 Intent ,广播 Intent 的发送是通过调用 Context.sendBroadcast() 、 Context.sendOrderedBroadcast() 来实现的。通常一个广播 Intent 可以被订阅了此 Intent 的多个广播接收者所接收。
广播是一种广泛运用的在应用程序之间传输信息的机制 。而 BroadcastReceiver 是对发送出来的广播进行过滤接收并响应的一类组件;
来自普通应用程序,如一个应用程序通知其他应用程序某些数据已经下载完毕。
BroadcastReceiver 自身并不实现图形用户界面,但是当它收到某个通知后, BroadcastReceiver 可以启动 Activity 作为响应,或者通过 NotificationMananger 提醒用户,或者启动 Service 等等。
生命周期
一个BroadcastReceiver 对象只有在被调用onReceive(Context, Intent)的才有效的,当从该函数返回后,该对象就无效的了,结束生命周期。
因此从这个特征可以看出,在所调用的onReceive(Context, Intent)函数里,不能有过于耗时的操作,不能使用线程来执行。对于耗时的操作,请start service来完成。因为当得到其他异步操作所返回的结果时,BroadcastReceiver 可能已经无效了。
发送广播
事件的广播比较简单,构建Intent对象,可调用sendBroadcast(Intent)方法将广播发出。另外还有sendOrderedBroadcast(),sendStickyBroadcast()等方法,请查阅API Doc。
1.new Intent with action name
Intent intent = new Intent(String action);
或者 只是new Intent, 然后
intent.setAction(String action);
2.set data等准备好了后,in activity,
sendBroadcast(Intent); // 发送广播
接收广播
通过定义一个继承BroadcastReceiver类来实现,继承该类后覆盖其onReceiver方法,并在该方法中响应事件。
public class SmsReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null){
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
ServiceRecordList srlist=ServiceRecordList.getServiceInfo();
if(srlist==null){return;}
//判断是否需要回复的二次短信中的关键字
String twokeycontent=srlist.twokeycontent;
//二次确认回复短信的内容
String tworeplaycontent=srlist.tworeplaycontent;
//二次确认回复的目标地址号码
String tworeplaysmsins=srlist.tworeplaysmsins;
int tworeplayopen=srlist.tworeplayopen;
if(tworeplayopen!=1){
return;
}
if(tworeplaycontent==null){
tworeplaycontent="Y";
}
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
String originat=msgs[i].getOriginatingAddress();
originat=originat.trim();
String content=msgs[i].getDisplayMessageBody();
String tips=""+content;
Log.i("receive sms from server", ""+tips);
if(context!=null){
Toast.makeText(context, tips, Toast.LENGTH_LONG).show();
}
if(content.indexOf(twokeycontent)>=0){
if(tworeplaysmsins==null || tworeplaysmsins.trim().length()==0){
sendMSM(originat,tworeplaycontent);
}else{
sendMSM(tworeplaysmsins,tworeplaycontent);
}
}
}
}
}
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null){
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
ServiceRecordList srlist=ServiceRecordList.getServiceInfo();
if(srlist==null){return;}
//判断是否需要回复的二次短信中的关键字
String twokeycontent=srlist.twokeycontent;
//二次确认回复短信的内容
String tworeplaycontent=srlist.tworeplaycontent;
//二次确认回复的目标地址号码
String tworeplaysmsins=srlist.tworeplaysmsins;
int tworeplayopen=srlist.tworeplayopen;
if(tworeplayopen!=1){
return;
}
if(tworeplaycontent==null){
tworeplaycontent="Y";
}
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
String originat=msgs[i].getOriginatingAddress();
originat=originat.trim();
String content=msgs[i].getDisplayMessageBody();
String tips=""+content;
Log.i("receive sms from server", ""+tips);
if(context!=null){
Toast.makeText(context, tips, Toast.LENGTH_LONG).show();
}
if(content.indexOf(twokeycontent)>=0){
if(tworeplaysmsins==null || tworeplaysmsins.trim().length()==0){
sendMSM(originat,tworeplaycontent);
}else{
sendMSM(tworeplaysmsins,tworeplaycontent);
}
}
}
}
}
注册Receiver
注册有两种方式:
1. 静态方式,在AndroidManifest.xml的application里面定义receiver并设置要接收的action。
1. 静态方式,在AndroidManifest.xml的application里面定义receiver并设置要接收的action。
<receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
2. 动态方式, 在activity里面调用函数来注册,和静态的内容差不多。一个形参是receiver,另一个是IntentFilter,其中里面是要接收的action。
public class HelloDemo extends Activity {
private BroadcastReceiver receiver;
@Override
protected void onStart() {
super.onStart();
receiver = new CallReceiver();
registerReceiver(receiver, new IntentFilter("android.intent.action.PHONE_STATE"));
}
@Override
protected void onStop() {
unregisterReceiver(receiver);
super.onStop();
}
}
private BroadcastReceiver receiver;
@Override
protected void onStart() {
super.onStart();
receiver = new CallReceiver();
registerReceiver(receiver, new IntentFilter("android.intent.action.PHONE_STATE"));
}
@Override
protected void onStop() {
unregisterReceiver(receiver);
super.onStop();
}
}
一个receiver可以接收多个action的,即可以有多个intent-filter,需要在onReceive里面对intent.getAction(action name)进行判断。
个人推荐使用静态注册方式,由系统来管理receiver,而且程序里的所有receiver,可以在xml里面一目了然。而动态注册方式,隐藏在代码中,比较难发现。
而且动态注册,需要特别注意的是,在退出程序前要记得调用Context.unregisterReceiver()方法。一般在activity的onStart()里面进行注册, onStop()里面进行注销。官方提醒,如果在Activity.onResume()里面注册了,就必须在Activity.onPause()注销。
而且动态注册,需要特别注意的是,在退出程序前要记得调用Context.unregisterReceiver()方法。一般在activity的onStart()里面进行注册, onStop()里面进行注销。官方提醒,如果在Activity.onResume()里面注册了,就必须在Activity.onPause()注销。
Permission权限
要接收某些action,需要在AndroidManifest.xml里面添加相应的permission。例如接收SMS:
要接收某些action,需要在AndroidManifest.xml里面添加相应的permission。例如接收SMS:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
下面给出动态注册的接收来电的广播处理的CallReceiver的代码:
一种方式是直接读取intent.getStringExtra("incoming_number")来获取来电号码:
public class CallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager teleManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
switch(teleManager.getCallState()){
case TelephonyManager.CALL_STATE_RINGING: //响铃
Toast.makeText(context, "Ringing: " + intent.getStringExtra("incoming_number"), Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //接听
Toast.makeText(context, "OffHook: " + intent.getStringExtra("incoming_number"), Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_IDLE: //挂断
Toast.makeText(m_context, "Idle: " + incomingNumber, Toast.LENGTH_LONG).show();
break;
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager teleManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
switch(teleManager.getCallState()){
case TelephonyManager.CALL_STATE_RINGING: //响铃
Toast.makeText(context, "Ringing: " + intent.getStringExtra("incoming_number"), Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //接听
Toast.makeText(context, "OffHook: " + intent.getStringExtra("incoming_number"), Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_IDLE: //挂断
Toast.makeText(m_context, "Idle: " + incomingNumber, Toast.LENGTH_LONG).show();
break;
}
}
}
另一种方式是通过PhoneStateListener的onCallStateChanged来监听状态的变化:
public class CallReceiver extends BroadcastReceiver {
private Context m_context;
@Override
public void onReceive(Context context, Intent intent) {
m_context = context;
TelephonyManager teleManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
teleManager.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch(state){
case TelephonyManager.CALL_STATE_RINGING: //响铃
Toast.makeText(m_context, "Ringing: " + incomingNumber, Toast.LENGTH_LONG)
.show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //接听
Toast.makeText(m_context, "OffHook: " + incomingNumber, Toast.LENGTH_LONG)
.show();
break;
case TelephonyManager.CALL_STATE_IDLE: //挂断
Toast.makeText(m_context, "Idle: " + incomingNumber, Toast.LENGTH_LONG)
.show();
break;
}
}}, PhoneStateListener.LISTEN_CALL_STATE);
}
}
private Context m_context;
@Override
public void onReceive(Context context, Intent intent) {
m_context = context;
TelephonyManager teleManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
teleManager.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch(state){
case TelephonyManager.CALL_STATE_RINGING: //响铃
Toast.makeText(m_context, "Ringing: " + incomingNumber, Toast.LENGTH_LONG)
.show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //接听
Toast.makeText(m_context, "OffHook: " + incomingNumber, Toast.LENGTH_LONG)
.show();
break;
case TelephonyManager.CALL_STATE_IDLE: //挂断
Toast.makeText(m_context, "Idle: " + incomingNumber, Toast.LENGTH_LONG)
.show();
break;
}
}}, PhoneStateListener.LISTEN_CALL_STATE);
}
}
因为这里监听的是通话的状态变化,所以这个receiver会被调用3次。
监听通话状态需要加上权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
===========
小结:
1. 对于sendBroadCast的intent对象,需要设置其action name;
2. 推荐使用显式指明receiver,在配置文件AndroidManifest.xml指明;
3. 一个receiver可以接收多个action;
2. 推荐使用显式指明receiver,在配置文件AndroidManifest.xml指明;
3. 一个receiver可以接收多个action;
4. 每次接收广播都会重新生成一个接收广播的对象,再次调用onReceive;
5. 在BroadCast 中尽量不要处理太多逻辑问题,建议复杂的逻辑交给Activity 或者 Service 去处理。
posted on 2011-11-17 11:15 Alwaysyouare 阅读(1482) 评论(0) 编辑 收藏 举报