android自动验证码填写

安卓开发   2016-04-06 16:39:43 发布
您的评价:
     
4.0
收藏     3收藏
文件夹
标签
(多个标签用逗号分隔)

首先定义一个广播接收者接受系统短信广播,对收到短信的广播进行判断,如果是目标号码就进行验证码的解析,并实现接口返回给调用处。

import android.content.BroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsMessage;


import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SMSReceiver extends BroadcastReceiver

{


    public interface ISMSListener {

        public void onSmsReceive(String verifyCode);


    }

    private static ISMSListener mSMSListener;

    public SMSReceiver(ISMSListener ismsListener) {

        mSMSListener = ismsListener;
    }

    public static final String TAG = "ImiChatSMSReceiver";

//android.provider.Telephony.SMSReceiver.Intents

    public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";


    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(SMS_RECEIVED_ACTION)) {
            SmsMessage[] messages = getMessagesFromIntent(intent);
            for (SmsMessage message : messages) {

 //                LogUtil.d(message.getOriginatingAddress() + " : " +
//
//                 message.getDisplayOriginatingAddress() + " : " +
//
//                message.getDisplayMessageBody() + " : " +
//
//                message.getTimestampMillis());
//                              在这里你可以对放松的号码进行判断,只接受目标号码的验证码
                String msg = message.getDisplayMessageBody();

                String verifyCode = null;
                Pattern p = Pattern.compile("\\d{6}");//这里你可以更改数字来指定验证码的位数
                Matcher m = p.matcher(msg);
                while (m.find()) {
                    verifyCode = m.group();
                    break;
                }

                if (mSMSListener != null) {
                    mSMSListener.onSmsReceive(verifyCode);
                }
            }

        }

    }

    public final SmsMessage[] getMessagesFromIntent(Intent intent) {
        Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
        byte[][] pduObjs = new byte[messages.length][];
        for (int i = 0; i < messages.length; i++) {
            pduObjs[i] = (byte[]) messages[i];
        }
        byte[][] pdus = new byte[pduObjs.length][];
        int pduCount = pdus.length;
        SmsMessage[] msgs = new SmsMessage[pduCount];
        for (int i = 0; i < pduCount; i++) {
            pdus[i] = pduObjs[i];
            msgs[i] = SmsMessage.createFromPdu(pdus[i]);
        }
        return msgs;
    }

}

Activity实现接口并注册广播。

public class MainActivity extends AppCompatActivity implements SMSReceiver.ISMSListener {
private SMSReceiver mReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         mReceiver =new SMSReceiver(this);

        IntentFilter filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");

        registerReceiver(mReceiver,filter);
    }

    @Override
    public void onSmsReceive(String verifyCode) {
        Toast.makeText(this, "验证码为:" + verifyCode, Toast.LENGTH_SHORT).show();
//获取验证码并填写到EditText中,即可
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }
}

添加权限:

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

OK搞定。

来自: http://blog.flyou.ren/?p=247

扩展阅读

2015最流行的Android组件、工具、框架大全
DevOps on Android: 加速 App 从代码到上线
Android APK加固技术方案调研
android自动验证码填写
Android 短信验证码自动填写

为您推荐

Android学习笔记之广播(2)
Android利用BroadcastReceiver实现短信监听
Android短信拦截
Android拦截短信并屏蔽系统的Notification
Android中handler的作用与线程

更多

安卓开发
Android开发
相关文档  — 更多
相关经验  — 更多
相关讨论  — 更多