天天看点

短信工具类 SmsUtil

只需要 服务器 获取验证码接口

布局中 设置一个 输入框 一个按钮 就可以 获取验证码后倒计时 并将短信自动填写到输入框

/**
 * 获取验证码工具
 * 功能:获取验证码 按钮倒计时, 自动填写验证码
 * Created by my on 2016/4/21.
 */
public class VcodeUtils {
    /**
     * 获取验证码
     *
     * @param
     */
    private Button mBt_vcode;
    private EditText mEtVcode;
    private Activity acitvity;
    private SMSReceiver mSmsReceiver;


    /**
     * @param mBt_vcode 倒计时的button
     * @param acitvity
     */
    public VcodeUtils(Button mBt_vcode, Activity acitvity,EditText mEtVcode ) {
        this.mBt_vcode = mBt_vcode;
        this.acitvity = acitvity;
        this.mEtVcode=mEtVcode;
        registSmsReciver();
    }
    private CountDownTimer timer = new CountDownTimer(, ) {

        @Override
        public void onTick(long millisUntilFinished) {
            //  btVcode.setText("重新发送(" + msg.arg1 + ")");
            mBt_vcode.setEnabled(false);
            mBt_vcode.setTextColor(Color.GRAY);
            mBt_vcode.setText((millisUntilFinished / ) + "秒后可重发");
        }

        @Override
        public void onFinish() {
            mBt_vcode.setEnabled(true);
            mBt_vcode.setTextColor(Color.parseColor("#ff4e8de3"));
            mBt_vcode.setText("获取验证码");
        }
    };
    public void getVcode(String url) {
        final Dialog progressDialog = CustomProgress.show(acitvity, "加载中", true, null);
        OkHttpUtils.get()
                .url(url)
                .addHeader("DeviceID", MyApplication.DeviceID)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {
                if (progressDialog != null) {
                    progressDialog.dismiss();
                }
                Toast.makeText(acitvity, "获取验证码失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {
                if (progressDialog != null) {
                    progressDialog.dismiss();
                }
                String result = StreamUtils.decodeUnicode(response);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(result);
                    String code = jsonObject.getString("code");
                    if (code.equals("200")) {
                        Toast.makeText(acitvity, "获取验证码成功", Toast.LENGTH_SHORT).show();
                        timer.start();
                    } else {
                        Toast.makeText(acitvity, "获取验证码失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

    }
    public static boolean checkMobile(String mobile) {
        Pattern pattern = Pattern.compile("^1\\d{10}");
        Matcher matcher = pattern.matcher(mobile);
        if (matcher.find()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 注册短信广播接收者
     */
    private  void registSmsReciver() {
        mSmsReceiver = new SMSReceiver();
        mSmsReceiver.setMessageListener(new SMSReceiver.MessageListener() {
            @Override
            public void onReceived(String cont) {
                mEtVcode.setText(cont);
            }
        });
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
        intentFilter.setPriority();
        acitvity.registerReceiver(mSmsReceiver,intentFilter);//注册
    }

    /**
     * 必须在合适位置调用此方法  解绑广播接收者
     * 可以再 onDestroy() 中使用该函数
     */
    public   void unregisterReceiver(){
        if(mSmsReceiver!=null){
            acitvity.unregisterReceiver(mSmsReceiver);
            mSmsReceiver=null;
        }
    }
}