zoukankan      html  css  js  c++  java
  • 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>
    

     即可实现所需功能

  • 相关阅读:
    net core 使用 rabbitmq
    asp.net core WebApi 返回 HttpResponseMessage
    asp.net core 2.1 WebApi 快速入门
    JQuery EasyUI combobox动态添加option
    php截取字符去掉最后一个字符
    JQuery EasyUI Combobox的onChange事件
    对于不返回任何键列信息的 selectcommand 不支持 updatecommand 的动态 sql 生成
    Access2007 操作或事件已被禁用模式阻止解决办法
    Easyui 中 Tabsr的常用方法
    Win 7 IE11不能下载文件,右键另存为也不行
  • 原文地址:https://www.cnblogs.com/loaderman/p/6542720.html
Copyright © 2011-2022 走看看