• android中如何自动获取短信验证码


    package com.loaderman.smsbroadcastreceiver;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.telephony.SmsMessage;
    import android.text.TextUtils;
    import android.util.Log;
    import android.widget.EditText;
    
    @SuppressLint("HandlerLeak")
    public class MainActivity extends Activity {
    	private BroadcastReceiver smsReceiver;
    	private IntentFilter filter2;
    	private Handler handler;
    	private EditText et;
    	private String strContent;
    	private String patternCoder = "(?<!\d)\d{6}(?!\d)";
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		et = (EditText) findViewById(R.id.et);
    		handler = new Handler() {
    			public void handleMessage(android.os.Message msg) {
    				et.setText(strContent);
    			};
    		};
    		filter2 = new IntentFilter();
    		filter2.addAction("android.provider.Telephony.SMS_RECEIVED");
    		filter2.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);
    					// Time time = new Time();
    					// time.set(sms.getTimestampMillis());
    					// String time2 = time.format3339(true);
    					// Log.d("logo", from + "   " + message + "  " + time2);
    					// strContent = from + "   " + message;
    					// handler.sendEmptyMessage(1);
    					if (!TextUtils.isEmpty(from)) {
    						String code = patternCode(message);
    						if (!TextUtils.isEmpty(code)) {
    							strContent = code;
    							handler.sendEmptyMessage(1);
    						}
    					}
    				}
    			}
    		};
    		registerReceiver(smsReceiver, filter2);
    	}
    
    	@Override
    	protected void onDestroy() {
    		super.onDestroy();
    		unregisterReceiver(smsReceiver);
    	}
    
    	/**
    	 * 匹配短信中间的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;
    	}
    }
    

     添加权限:

     <uses-permission android:name="android.permission.RECEIVE_SMS" />
    

     activity_main.xml

    <RelativeLayout 
    	xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >
    
        <EditText
            android:id="@+id/et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center_horizontal"
            android:hint="@string/smscontent" />
    </RelativeLayout>
    

     即可实现所需功能

  • 相关阅读:
    行坐标HDU1175:连连看(DFS)
    hibernatesqlHibernate中createQuery与createSQLQuery两者的区别
    代码窗口九种方式实现 javascripts 弹出对话框窗口代码
    函数线程VC++多线程函数调用
    错误classspring错误汇总
    android下载手动下载Android SDK
    节点node[算法导论]二叉排序树(Binary Search Tree)
    共享访问失败解决(无法访问 您可能没有权限使用网络资源)
    DnsTester(DNS 服务器性能测试工具)
    vc 花指令 防破解
  • 原文地址:https://www.cnblogs.com/loaderman/p/6542720.html
Copyright © 2020-2023  润新知