本文實(shí)例為大家分享了Android自動(dòng)填充短信驗(yàn)證碼的具體代碼,供大家參考,具體內(nèi)容如下
短信驗(yàn)證碼是大部分軟件里面都存在的功能,同時(shí)為了避免用戶自己輸入導(dǎo)致的繁瑣操作,有一部分app設(shè)計(jì)者將其設(shè)置成了自動(dòng)填充的方式,方便用戶操作那么這種方式是什么實(shí)現(xiàn)的呢。
利用廣播接收器來(lái)攔截短信獲取其中匹配的內(nèi)容,提供回掉,將短信內(nèi)容暴露到activity中實(shí)現(xiàn)自動(dòng)填充
首先我們要實(shí)現(xiàn)一個(gè)廣播接收器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
package com.wquant.weilt.reciver; import java.util.regex.Matcher; import java.util.regex.Pattern; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Message; import android.telephony.SmsMessage; import android.text.TextUtils; import android.util.Log; public class SmsReciver extends BroadcastReceiver { private String patternCoder = "(?<!\\d)\\d{6}(?!\\d)" ; @Override public void onReceive(Context context, Intent intent) { //獲取短信數(shù)據(jù) Object[] objs = (Object[]) intent.getExtras().get( "pdus" ); for (Object obj : objs) { byte [] pdu = ( byte []) obj; //將字節(jié)數(shù)組封裝成為smsmessage對(duì)象 SmsMessage sms = SmsMessage.createFromPdu(pdu); //獲得短短信內(nèi)容 String message = sms.getMessageBody(); Log.d( "短信內(nèi)容" , "message:" + message); // 短息的手機(jī)號(hào)。。+86開(kāi)頭? String from = sms.getOriginatingAddress(); Log.d( "短信來(lái)源" , "from :" + from); if (!TextUtils.isEmpty(from)) { String code = patternCode(message); if (!TextUtils.isEmpty(code)) { mMessageListener.onReceived(code); } } } } /** * 匹配短信中間的6個(gè)數(shù)字(驗(yàn)證碼等) * * @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 ; } // 回調(diào)接口 public interface MessageListener { public void onReceived(String message); } MessageListener mMessageListener; public void setOnReceivedMessageListener(MessageListener messageListener) { this .mMessageListener = messageListener; } } |
ok上面我們已經(jīng)實(shí)現(xiàn)了廣播接收器,在activity中我們要綁定一個(gè)意圖過(guò)濾器并將此廣播注冊(cè)在destory方法中要將其解除注冊(cè)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package com.wquant.weilt.controler; import org.apache.http.Header; import org.json.JSONException; import org.json.JSONObject; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.CountDownTimer; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.loopj.android.http.RequestParams; import com.wquant.weilt.MyApplication; import com.wquant.weilt.R; import com.wquant.weilt.control.base.JsonHttpResponseHandlerBase; import com.wquant.weilt.control.base.MyBaseActivity; import com.wquant.weilt.reciver.SmsReciver; import com.wquant.weilt.reciver.SmsReciver.MessageListener; import com.wquant.weilt.util.CToast; import com.wquant.weilt.util.CommonUtil; import com.wquant.weilt.util.Constant; import com.wquant.weilt.util.HttpUtil; /** * 修改密碼 * * @author zhaomy * */ public class RestartLoginOrTradPwdActivity extends MyBaseActivity { SmsReciver reciver; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super .onCreate(savedInstanceState); setContentView(R.layout.activity_restart_login_or_trad); init(); } private void init() { code = (EditText) findViewById(R.id.register_code); reciver= new SmsReciver(); IntentFilter filter = new IntentFilter(); // 設(shè)置短信攔截參數(shù) filter.addAction( "android.provider.Telephony.SMS_RECEIVED" ); //設(shè)置最大優(yōu)先級(jí) filter.setPriority(Integer.MAX_VALUE); registerReceiver(reciver, filter); reciver.setOnReceivedMessageListener( new MessageListener() { @Override public void onReceived(String message) { code.setText(message); } }); } @Override protected void onPause() { super .onPause(); } @Override protected void onDestroy() { unregisterReceiver(reciver); super .onDestroy(); } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/Applicaton/article/details/51720300