動態廣播監聽是在Activity,經過代碼調用registerReceiver和unregisterReceiver來註冊或者釋註銷聽對象android
表態廣播監聽是在AndoridMainfest.xml文件去註冊一個廣播監聽,由系統去註銷監聽數組
總結:動態與靜態如何區分,捉住是經過代碼仍是.xml去配置app
需求:我有一個註冊頁面,裏面有個驗證碼字段是會發送到手機,我要在activity裏監聽驗證短信並獲取驗證碼ide
注:要接收短信監聽,要授於短信接收的用戶權限<uses-permissionandroid:name="android.permission.RECEIVE_SMS"/>this
而後註冊廣播spa
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配置以下.net
<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,已經幫咱們過慮其它廣播類型了。線程