截取短信验证码,实现自动填写验证码

一个小功能实现短线验证码验自动填写功能。

拿到这个功能时,首先需要怎么去拦截获取手机短信。就用观察着来观察系统里的短消息的数据库的变化,“表“内容观察者,只要数据库发生变化,都会出发该ContentObserver 的派生类。

  于是,首先需要一个SMSContentObserver的监听类:

import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.net.Uri;
import android.os.Handler;
import net.panatrip.debugtoolbox.log.Log;

import net.panatrip.biqu.manager.SMSManager;

import java.lang.ref.WeakReference;


//用来观察系统里短消息的数据库变化  ”表“内容观察者,只要信息数据库发生变化,都会触发该ContentObserver 派生类  
public class SMSContentObserver extends ContentObserver {
    private static String TAG = "SMSContentObserver";
    private WeakReference mContext;
    private Handler mHandler;   //更新UI线程

    public SMSContentObserver(Context context, Handler handler) {
        super(handler);
        mContext = new WeakReference(context);
        mHandler = handler;
    }

    /**
     * 当所监听的Uri发生改变时,就会回调此方法
     *
     * @param selfChange 此值意义不大 一般情况下该回调值false
     */
    @Override
    public void onChange(boolean selfChange) {
        //查询收件箱里的内容
        Uri outSMSUri = Uri.parse("content://sms/inbox");
        Cursor c = mContext.get().getContentResolver().query(outSMSUri, null, null, null,"date desc");
        if (c != null && c.moveToFirst()) {
            try{
                Log.e(TAG, "the number of send is" + c.getString(c.getColumnIndex("address")) + "  " + c.getString(c.getColumnIndex("body")));
                StringBuilder sb = new StringBuilder();
                sb.append(c.getString(c.getColumnIndex("body")));
                mHandler.obtainMessage(SMSManager.MSG_OUTBOXCONTENT, sb.toString()).sendToTarget();
            }catch (CursorIndexOutOfBoundsException ex){
                ex.printStackTrace();
            }finally {
                c.close();
            }
        }
    }

}  
然后,我们需要一个接口回调去把拦截消息显示出去,需要一个开关去接受消息和关闭消息

public class SMSManager {
    private SMSContentObserver smsContentObserver;
    public static int MSG_OUTBOXCONTENT = 2;
    public static String ACTION = "android.provider.Telephony.SMS_RECEIVED";
    private static volatile SMSManager _instance = null;
    private IntentFilter intentFilter;
    private boolean startFlag = false;
    private  MessageListener mMessageListener;

    //回调接口
    public interface MessageListener {
        public void onReceived(String message);
    }

    private Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
            String outbox = (String) msg.obj;
            mMessageListener.onReceived(outbox);
        }
    };

    private SMSManager(){

    }

    public static SMSManager getInstance() {
        if (_instance == null) {
            synchronized (SMSManager.class) {
                if (_instance == null) {
                    _instance = new SMSManager();
                }
            }
        }
        return _instance;
    }

    /**
     * 开始接收
     *
     * @param context
     */
    public void startReceiveSMS(Context context,MessageListener listener) {
        smsContentObserver = new SMSContentObserver(context, mHandler);
        mMessageListener = listener;
        Uri smsUri = Uri.parse("content://sms");
        context.getContentResolver().registerContentObserver(smsUri, true,smsContentObserver );
    }


    /**
     * 停止接收
     *
     * @param context
     */
    public void stopReceiveSMS(Context context) {

      if(smsContentObserver!=null) {
            context.getContentResolver().unregisterContentObserver(smsContentObserver);
            smsContentObserver=null;
        }
    }



页面的调用

    SMSManager.getInstance().startReceiveSMS(getApplicationContext(), new SMSManager.MessageListener() {
        @Override
        public void onReceived(String message) {
            String code = BQUtils.patternCode(message);
            etCode.setText(code);
        }
    });
}




你可能感兴趣的:(截取短信验证码,实现自动填写验证码)