您好,登錄后才能下訂單哦!
package com.pzq.test.zhuanpan4; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Handler; import android.telephony.SmsMessage; import android.text.TextUtils; import android.util.Log; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Created by asus on 2016/6/26. */ public class SMSUtils { private BroadcastReceiver smsReceiver, codeReceiver; private IntentFilter filter, mIntentFilter; private Handler handler; private String strContent; private String patternCoder = "(?<!\\d)\\d{6}(?!\\d)"; private Activity mActivity; public static String MSMCode_CHANGE_BROADCAST = "com.pzq.test.SMSCode.CHANGE_BROADCAST"; public SMSUtils(Activity mActivity) { Log.d("mainpzqtest....", "getSMScode....444444444444444444444"); this.mActivity = mActivity; // getSMScode("555"); filter = new IntentFilter(); filter.addAction("android.provider.Telephony.SMS_RECEIVED"); filter.setPriority(Integer.MAX_VALUE); smsReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Object[] objs = (Object[]) intent.getExtras().get("pdus"); for (Object obj : objs) { byte[] pdu = (byte[]) obj; SmsMessage sms = SmsMessage.createFromPdu(pdu); // 短信的內容 String message = sms.getMessageBody(); Log.d("logo", "message " + message); // 短息的手機號。。+86開頭? String from = sms.getOriginatingAddress(); Log.d("logo", "from " + from); if (!TextUtils.isEmpty(from)) { String code = patternCode(message); if (!TextUtils.isEmpty(code)) { strContent = code; getSMScode(code); } } } } }; mActivity.registerReceiver(smsReceiver, filter); } /** * 匹配短信中間的6個數字(驗證碼等) * * @param patternContent * @return */ private String patternCode(String patternContent) { if (TextUtils.isEmpty(patternContent)) { return null; } Pattern p = Pattern.compile(patternCoder); Matcher matcher = p.matcher(patternContent); if (matcher.find()) { return matcher.group(); } return null; } public void getSMScode(String code) { Intent intent = new Intent(); intent.putExtra("code", code); intent.setAction(MSMCode_CHANGE_BROADCAST); mActivity.sendBroadcast(intent); } /*** * 使用 加權限 <uses-permission android:name="android.permission.RECEIVE_SMS" /> * * registerReceiver(); u = new SMSUtils(MainActivity.this); * private void registerReceiver() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(SMSUtils.MSMCode_CHANGE_BROADCAST); registerReceiver(new SMSBroadcastReceiver(), intentFilter); } class SMSBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction() == SMSUtils.MSMCode_CHANGE_BROADCAST) { Bundle bundle = intent.getExtras(); smScode = bundle.getString("code"); Log.d("mainpzqtest....", "mBroadcastReceiver...." + smScode); et.setText(smScode); } } }; * * */ }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。