天天看點

短信工具類 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;
        }
    }
}