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 等等。
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);
}
}
}
}
}
1. 静态方式,在AndroidManifest.xml的application里面定义receiver并设置要接收的action。
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
2. 动态方式, 在activity里面调用函数来注册,和静态的内容差不多。一个形参是receiver,另一个是IntentFilter,其中里面是要接收的action。
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();
}
}
而且动态注册,需要特别注意的是,在退出程序前要记得调用Context.unregisterReceiver()方法。一般在activity的onStart()里面进行注册, onStop()里面进行注销。官方提醒,如果在Activity.onResume()里面注册了,就必须在Activity.onPause()注销。
要接收某些action,需要在AndroidManifest.xml里面添加相应的permission。例如接收SMS:
下面给出动态注册的接收来电的广播处理的CallReceiver的代码:
@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;
}
}
}
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);
}
}
2. 推荐使用显式指明receiver,在配置文件AndroidManifest.xml指明;
3. 一个receiver可以接收多个action;