验证码mob集成&本地验证生成&短信截获

1.本地自定义view生成验证码:

这个是实现代码:

public class YANZHENGMA extends View{

    private Paint paint;
    private int viewWidth;
    private int viewHeight;
    private String [] codes=new String[4];

    public String[] getCodes() {
        return codes;
    }

    public YANZHENGMA(Context context) {
        super(context);
        init();
    }

    public YANZHENGMA(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public YANZHENGMA(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFakeBoldText(RandomUtil.getboolen());
        paint.setTextSize(100);
        paint.setStrokeWidth(10);
        paint.setShader(null);
        setBackgroundColor(RandomUtil.getBgRandomColor());
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                invalidate();
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//获取高度
        viewWidth=getWidth();
        viewHeight=getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
      //生成背景色

        //生成随机干扰线
        paint.setColor(RandomUtil.getTextRandomColor());
        //int linenum=RandomUtil.getRandomInt(4);
        for (int i = 0; i <4 ; i++) {
            paint.setColor(RandomUtil.getTextRandomColor());
            canvas.drawLine(RandomUtil.getRandomFloat(viewWidth),RandomUtil.getRandomFloat(viewHeight),RandomUtil.getRandomFloat(viewWidth),RandomUtil.getRandomFloat(viewHeight),paint);
        }
        //生成干扰点
        int pointnum=RandomUtil.getRandomInt(20);
        for (int i = 0; i <20 ; i++) {
            paint.setColor(RandomUtil.getTextRandomColor());
            canvas.drawPoint(RandomUtil.getRandomFloat(viewWidth),RandomUtil.getRandomFloat(viewHeight),paint);
        }
        //生成文字
        for (int i = 0; i < 4; i++) {
            paint.setTextSkewX(RandomUtil.getRandomFloat(1)-0.5f);
            paint.setColor(RandomUtil.getTextRandomColor());
            float textX= RandomUtil.getRandomFloat(4)+viewWidth/4*i+viewWidth/8;
            float textY= RandomUtil.getRandomFloat(20)-10-10+viewHeight;
            codes[i]=RandomUtil.getRandomText()+"";
            canvas.drawText(codes[i] ,textX,textY,paint);
        }
    }


}
这个是随机工具类:

public class RandomUtil {
    static Random mRandom=new Random();
    //生成随机字符串
    public static char getRandomText() {

        int i = mRandom.nextInt(42) + 48;
        while (i > 57 && i < 65) {
            i = mRandom.nextInt(42) + 48;
        }
        char tmp = (char) (i);
        return tmp;
    }
    //生成背景色
    public static int getBgRandomColor() {

        int r = mRandom.nextInt(140) + 115;
        int g = mRandom.nextInt(140) + 115;
        int b = mRandom.nextInt(140) + 115;
        return Color.rgb(r, g, b);
    }
    //字体颜色
    public static int getTextRandomColor() {

        int r = mRandom.nextInt(90) + 40;
        int g = mRandom.nextInt(90) + 40;
        int b = mRandom.nextInt(90) + 40;
        return Color.rgb(r, g, b);
    }
    //android设置Typeface字体
    public static Typeface getType(){
        //Typeface.defaultFromStyle(Typeface)
        return null;
    }
    //随机boolen
    public static boolean getboolen() {

        return mRandom.nextBoolean();
    }
    //随机int
    public static int getRandomInt(int a) {
     //   rand

        return mRandom.nextInt(a);
    }
    //随机float
    public static float getRandomFloat(int a) {
        //   rand
        return mRandom.nextInt(a*100)/100f;
    }
}
2.这个是第三方的短信验证码使用的是mob的第三方短信验证码

需要做的操作将arr包和第三方库导入


gradle文件中配置:

repositories {
    flatDir{
        dirs 'libs'
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile files('libs/MobTools-2017.0321.1624.jar')
    //短信验证码依赖
    compile name:'SMSSDK-2.1.4',ext:'aar'
    compile name:'SMSSDKGUI-2.1.4',ext:'aar'
}


application中初始化

public class APP extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        SMSSDK.initSDK(this, "1e3249818fa90", "4f8cb6e6ea65cdfd878798701362f6aa");
    }
}

代码中使用打开登录(网上找的方法比较清晰):

  public void getyanzhen(View view) {

        //打开注册页面
        RegisterPage registerPage = new RegisterPage();
       // registerPage.
        registerPage.setRegisterCallback(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
// 解析注册结果
                if (result == SMSSDK.RESULT_COMPLETE) {
                    @SuppressWarnings("unchecked")
                    HashMap<String,Object> phoneMap = (HashMap<String, Object>) data;
                    String country = (String) phoneMap.get("country");
                    String phone = (String) phoneMap.get("phone");

// 提交用户信息(此方法可以不调用)
                    registerUser(country, phone);
                }
            }
        });
        registerPage.show(this);
    }

3.自己写后台接入时,在app中获取验证码:

    private SmsObserver mSmsObserver;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 1) {
                String code = (String) msg.obj;
               // mSmsTV.setText(code);
                Log.e( "handleMessage: ",code );
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSmsObserver = new SmsObserver(this, mHandler);
        Uri smsUri = Uri.parse("content://sms");
        getContentResolver().registerContentObserver(smsUri, true, mSmsObserver);//注册短信uri的监
    }

    @Override
    protected void onPause() {
        super.onPause();
        getContentResolver().unregisterContentObserver(mSmsObserver);
    }

public class SmsObserver extends ContentObserver {
    private Context mContext;
    private Handler mHandler;//更新ui

    public SmsObserver(Context context, Handler handler) {
        super(handler);
        mContext = context;
        mHandler = handler;

    }

    @Override
    public void onChange(boolean selfChange, Uri uri) {
        super.onChange(selfChange, uri);

        /* 收到一条短信,onChange()会执行两次,uri不同 */
        if (uri.toString().equals("content://sms/raw")) {//未写入数据库,不进行任何操作
            return;
        } else {//此时已经写入数据库
            Uri inboxUri = Uri.parse("content://sms/inbox");
            /* 按照日期倒序排序 */
            Cursor cursor = mContext.getContentResolver().query(inboxUri, null, null, null, "date desc");
            if (cursor != null) {
                if (cursor.moveToFirst()) {//游标移动到first位置
                    /* 发件人的号码 */
                    String address = cursor.getString(cursor.getColumnIndex("address"));
                    /* 短信内容 */
                    String body = cursor.getString(cursor.getColumnIndex("body"));
                    Log.i("wyk", "address:" + address + ",body:" + body);

                    /* 利用正则提取验证码(根据实际情况修改) */
                    Pattern pattern = Pattern.compile("(\\d{6})");//提取六位数字
                    Matcher matcher = pattern.matcher(body);//进行匹配
                    if(matcher.find()){//匹配成功
                        String code = matcher.group(0);
                        Message msg = mHandler.obtainMessage(1, code);
                        mHandler.sendMessage(msg);
                        Log.i("wyk","code:"+code);
                    }
                }
                cursor.close();
            }
        }
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值