激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android實現自動填充短信驗證碼

Android實現自動填充短信驗證碼

2022-02-22 15:22Applicaton Android

這篇文章主要為大家詳細介紹了Android實現自動填充短信驗證碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android自動填充短信驗證碼的具體代碼,供大家參考,具體內容如下

短信驗證碼是大部分軟件里面都存在的功能,同時為了避免用戶自己輸入導致的繁瑣操作,有一部分app設計者將其設置成了自動填充的方式,方便用戶操作那么這種方式是什么實現的呢。

利用廣播接收器來攔截短信獲取其中匹配的內容,提供回掉,將短信內容暴露到activity中實現自動填充

首先我們要實現一個廣播接收器

?
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) {
  //獲取短信數據
  Object[] objs = (Object[]) intent.getExtras().get("pdus");
  for (Object obj : objs) {
   byte[] pdu = (byte[]) obj;
   //將字節數組封裝成為smsmessage對象
   SmsMessage sms = SmsMessage.createFromPdu(pdu);
   //獲得短短信內容
   String message = sms.getMessageBody();
   Log.d("短信內容", "message:" + message);
   // 短息的手機號。。+86開頭?
   String from = sms.getOriginatingAddress();
   Log.d("短信來源", "from :" + from);
   if (!TextUtils.isEmpty(from)) {
    String code = patternCode(message);
    if (!TextUtils.isEmpty(code)) {
     mMessageListener.onReceived(code);
    }
   }
  }
 }
 /**
  * 匹配短信中間的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 interface MessageListener {
  public void onReceived(String message);
 }
 
 MessageListener mMessageListener;
 
 public void setOnReceivedMessageListener(MessageListener messageListener) {
  this.mMessageListener = messageListener;
 }
}

ok上面我們已經實現了廣播接收器,在activity中我們要綁定一個意圖過濾器并將此廣播注冊在destory方法中要將其解除注冊

?
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();
  // 設置短信攔截參數
  filter.addAction("android.provider.Telephony.SMS_RECEIVED");
  //設置最大優先級
  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();
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/Applicaton/article/details/51720300

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品乱码久久久久 | 久久噜噜噜精品国产亚洲综合 | 日本精品免费观看 | 新久草在线视频 | 狠狠干五月天 | 毛片视频大全 | 亚洲影视在线 | chengrenzaixian| 亚洲va在线 | 国产精品亚洲欧美一级在线 | 黄色视屏免费在线观看 | 久久久久九九九女人毛片 | 在线成人一区二区 | 99久久精品免费 | 成人aaaaa片毛片按摩 | 国产精品午夜未成人免费观看 | 国产精品久久久免费 | 国产午夜免费福利 | 欧美14一15sex性hd | 日本精品婷婷久久爽一下 | 最新一区二区三区 | 亚洲精品一区中文字幕 | 久久av高清 | 成人三级电影在线 | 亚洲性在线视频 | 欧美中文字幕在线视频 | 色天天综合网 | 日本在线看 | 奇米888一区二区三区 | 成人免费在线视频 | 天天夜天天操 | av成人在线观看 | 奇米影视亚洲春色 | 亚洲精品一区二区三区免 | 全黄裸片武则天一级第4季 偿还电影免费看 | 日韩字幕在线观看 | 久国久产久精永久网页 | 手机免费看一级片 | 91久久夜色精品国产网站 | 亚洲精品久久久久久久久久 | 久久大胆视频 |