动态与静态广播截取短信
通俗理解动态与静态广播监听
动态广播监听是在Activity,通过代码调用registerReceiver和unregisterReceiver来注册或者释注销听对象
表态广播监听是在AndoridMainfest.xml文件去注册一个广播监听,由系统去注销监听
总结:动态与静态如何区分,捉住是通过代码还是.xml去配置
动态注册短信监听
需求:我有一个注册页面,里面有个验证码字段是会发送到手机,我要在activity里监听验证短信并获取验证码
注:要接收短信监听,要授于短信接收的用户权限<uses-permissionandroid:name="android.permission.RECEIVE_SMS"/>
然后注册广播
package com.test.testmsg; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.telephony.SmsMessage; import android.util.Log; public class MainActivity extends Activity { //表示接收短信广播 public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED"; //定义广播监听者 private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction();
//过滤广播,如果是接收信息的广播则做处理,否则不处理 if(action.equals(SMS_RECEIVED_ACTION)){ Bundle bundle = intent.getExtras(); if(bundle != null){ // 取消广播(这行代码将会让系统收不到短信) //abortBroadcast(); //pdu为承载着一条短信的所有短信。 //一条短信为140个英文字符长度,在这个长度范围内,即需一个pdu即可。超出这个范围,即要分割成多个pdu数组。 Object[] pdus = (Object[]) bundle.get("pdus"); for(Object pdu : pdus){ SmsMessage smsMessage = SmsMessage.createFromPdu((byte[])pdu); String address = smsMessage.getOriginatingAddress(); String body = smsMessage.getMessageBody(); Log.i("KwokTag", address + " " + body); }
//然后你可以截取短信中的验证码,通过handle发送一个消息让主线程的验证码填入验证码区域
/** ....... */
} } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //getLocalMsg(); IntentFilter filter = new IntentFilter(); filter.addAction(SMS_RECEIVED_ACTION); this.registerReceiver(receiver, filter); } //在Activity销毁时要注销监听器 @Override protected void onDestroy() { // TODO Auto-generated method stub this.unregisterReceiver(receiver); super.onDestroy(); } }
静态注册短信监听
package com.test.testmsg; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsMessage; import android.util.Log; public class MsgReceiver extends BroadcastReceiver { public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";public MsgReceiver() { } @Override public void onReceive(Context context, Intent intent) { // TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. String action = intent.getAction(); if(action.equals(SMS_RECEIVED_ACTION)){ Bundle bundle = intent.getExtras(); if(bundle != null){ //pdu为承载着一条短信的所有短信。 //一条短信为140个英文字符长度,在这个长度范围内,即需一个pdu即可。超出这个范围,即要分割成多个pdu数组。 Object[] pdus = (Object[]) bundle.get("pdus"); for(Object pdu : pdus){ SmsMessage smsMessage = SmsMessage.createFromPdu((byte[])pdu); String address = smsMessage.getOriginatingAddress(); String body = smsMessage.getMessageBody(); Log.i("KwokTag", address + " " + body); } } } } }
写一个广播监听,实现监听处理,然后在AndroidMainfest配置如下
<receiver android:name="com.test.testmsg.MsgReceiver" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver>
这样也是可以实现短信监听,其实在onReceive里面可以不用再判断if(action.equals(SMS_RECEIVED_ACTION)),因为我们添加了intent-filter,已经帮我们过虑其它广播类型了。