編輯:關於Android編程
本文實例講解了Android自動提取短信驗證碼解決方案,分享給大家供大家參考,具體內容如下
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> <uses-permission android:name="android.permission.READ_SMS"></uses-permission>
package com.dongdongsongcan.app; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.SmsMessage; import com.su.dolphin.utils.LogUtil; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SMSReceiver extends BroadcastReceiver { public interface ISMSListener { public void onSmsReceive(String verifyCode); } private static ISMSListener mSMSListener; public SMSReceiver(ISMSListener ismsListener) { mSMSListener = ismsListener; } public static final String TAG = "ImiChatSMSReceiver"; //android.provider.Telephony.Sms.Intents public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED"; @Override public void onReceive(Context context, Intent intent) { LogUtil.d(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); if (intent.getAction().equals(SMS_RECEIVED_ACTION)) { SmsMessage[] messages = getMessagesFromIntent(intent); for (SmsMessage message : messages) { // LogUtil.d(message.getOriginatingAddress() + " : " + // // message.getDisplayOriginatingAddress() + " : " + // // message.getDisplayMessageBody() + " : " + // // message.getTimestampMillis()); String msg = message.getDisplayMessageBody(); LogUtil.d("MSG: " + msg); String verifyCode = null; Pattern p = Pattern.compile("\\d{4}"); Matcher m = p.matcher(msg); while (m.find()) { verifyCode = m.group(); break; } LogUtil.d("verifyCode " + verifyCode); if (mSMSListener != null) { mSMSListener.onSmsReceive(verifyCode); } } } } public final SmsMessage[] getMessagesFromIntent(Intent intent) { Object[] messages = (Object[]) intent.getSerializableExtra("pdus"); byte[][] pduObjs = new byte[messages.length][]; for (int i = 0; i < messages.length; i++) { pduObjs[i] = (byte[]) messages[i]; } byte[][] pdus = new byte[pduObjs.length][]; int pduCount = pdus.length; SmsMessage[] msgs = new SmsMessage[pduCount]; for (int i = 0; i < pduCount; i++) { pdus[i] = pduObjs[i]; msgs[i] = SmsMessage.createFromPdu(pdus[i]); } return msgs; } }
以上就是本文的全部內容,希望對大家的學習有所幫助。
一、安裝及配置Genymotion(1)由於Eclipse中自帶的SDK模擬器,啟動之慢,不說了 現在給大家介紹一種比較快的模擬器Genymotion(2)首先去Geny
本文主要介紹一下如何使用CoordinatorLayout先看看官方是怎麼介紹Material Design的 We challenged ourselves to cr
效果說明:滑竿指示器,是一段彎曲的圓弧,要求在桿上,有滑動小球事件,小球會根據下標文字的起始角度與終止角度,是否選擇滑倒下一個位置。當點擊下標文字時,小球也要做出相應的
本文介紹九宮格程序的設計代碼。一.相關介紹(一)效果顯示1.程序剛運行時的效果:2.在頁面上點擊選擇並滑動時的畫面3.選擇密碼之後的顯示(二)功能介紹1.點擊某圓圈後,在
本文介紹本文是翻譯自Google 官方課程 Building Apps