您好,登錄后才能下訂單哦!
前言
短信驗證碼獲取并自動填寫現在已經成為一個人性化App的標配了,這篇文章將實現一個短信驗證碼獲取并自動填寫的demo。其實就是讀取指定號碼的短信并提取出驗證碼,然后賦值給EditText顯示。
demo效果圖:
讀取短信
Android系統在接受到一條短信的時候會發出一條Action為android.provider.Telephony.SMS_RECEIVED的有序廣播,因此我們讀取短信的驗證碼只需要監聽這個廣播然后提取出短信中的驗證碼即可,如:
/** * Created by 程龍 on 2018/9/27. */ public class SMSBroadcastReceiver extends BroadcastReceiver { private static final String TAG = "SMSBroadcastReceiver"; private OnReceiveSMSListener mOnReceiveSMSListener; public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED"; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(SMS_RECEIVED_ACTION)) { Object[] pdus = (Object[]) intent.getExtras().get("pdus"); for(Object pdu:pdus) { SmsMessage smsMessage = SmsMessage.createFromPdu((byte [])pdu); // 短信號碼 String sender = smsMessage.getDisplayOriginatingAddress(); Log.d(TAG,sender+""); //短信內容 String content = smsMessage.getDisplayMessageBody(); // 篩選 if ("短信發送方號碼".equals(sender) && mOnReceiveSMSListener!= null) { Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher(content); if(matcher.find()) { mOnReceiveSMSListener.onReceived(matcher.group()); } abortBroadcast(); } } } } /** * 回調接口 */ public interface OnReceiveSMSListener { void onReceived(String message); } public void setOnReceiveSMSListener(OnReceiveSMSListener onReceiveSMSListener) { mOnReceiveSMSListener = onReceiveSMSListener; } }
代碼注釋比較清楚,通過SmsMessage對象可以得到短信的發送號碼,短信內容及相關信息。當短信號碼為你要讀取的短信發送方號碼時用正則表達式提取出短信內容的驗證碼,然后通過調用監聽器的回調方法更新相應的UI。這里只需將短信驗證發送方號碼替換成相應的號碼即可,如“106903561008710”為騰訊云注冊發送的短信號碼。
通常,我們只會在注冊或登錄頁面讀取短信驗證碼,所以,我們采用動態注冊廣播的方式注冊這個廣播,如:
// 注冊廣播 IntentFilter intentFilter = new IntentFilter(SMSBroadcastReceiver.SMS_RECEIVED_ACTION); // 設置優先級 intentFilter.setPriority(Integer.MAX_VALUE); registerReceiver(mSMSBroadcastReceiver,intentFilter);
取消注冊
@Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mSMSBroadcastReceiver); }
MainActivity.java的代碼為:
public class MainActivity extends AppCompatActivity implements SMSBroadcastReceiver.OnReceiveSMSListener { private SMSBroadcastReceiver mSMSBroadcastReceiver = new SMSBroadcastReceiver(); private EditText mEt_code; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEt_code = (EditText) findViewById(R.id.et_code); mSMSBroadcastReceiver.setOnReceiveSMSListener(this); // 注冊廣播 IntentFilter intentFilter = new IntentFilter(SMSBroadcastReceiver.SMS_RECEIVED_ACTION); // 設置優先級 intentFilter.setPriority(Integer.MAX_VALUE); registerReceiver(mSMSBroadcastReceiver,intentFilter); } @Override public void onReceived(String message) { mEt_code.setText(message+""); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mSMSBroadcastReceiver); } }
這個demo的代碼實現比較簡單,就是監聽廣播,讀取收到的短信,然后過濾篩選出我們要的驗證碼短信,再用正則表達式提取到驗證碼設置給EditText顯示即可。大家下載或者自己編寫這個demo并更改發送方短信號碼運行,然后可以找個注冊網址或是模擬驗證碼短信進行測試。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。