短信验证的实现

首先写一个短信验证类RegisterPage

/** 短信注册页面*/
public class RegisterPage extends FakeActivity implements OnClickListener,
        TextWatcher {

    // 默认使用中国区号
    private static final String DEFAULT_COUNTRY_ID = "42";

    private EventHandler callback;

    // 国家
    private TextView tvCountry;
    // 手机号码
    private EditText etPhoneNum;
    // 国家编号
    private TextView tvCountryNum;
    // clear 号码
    private ImageView ivClear;
    // 下一步按钮
    private Button btnNext;

    private String currentId;
    private String currentCode;
    private EventHandler handler;
    private Dialog pd;
    private OnSendMessageHandler osmHandler;

    public void setRegisterCallback(EventHandler callback) {
        this.callback = callback;
    }

    public void setOnSendMessageHandler(OnSendMessageHandler h) {
        osmHandler = h;
    }

    public void show(Context context) {
        super.show(context, null);
    }

    public void onCreate() {

        RegisterPageLayout page = new RegisterPageLayout(activity);
        LinearLayout layout = page.getLayout();

        if (layout != null) {
            activity.setContentView(layout);
            currentId = DEFAULT_COUNTRY_ID;

            View llBack = activity.findViewById(Res.id.ll_back);
            TextView tv = (TextView) activity.findViewById(Res.id.tv_title);
            int resId = getStringRes(activity, "smssdk_regist");
            if (resId > 0) {
                tv.setText(resId);
            }

            View viewCountry = activity.findViewById(Res.id.rl_country);
            btnNext = (Button) activity.findViewById(Res.id.btn_next);
            tvCountry = (TextView) activity.findViewById(Res.id.tv_country);

            String[] country = getCurrentCountry();
            // String[] country = SMSSDK.getCountry(currentId);
            if (country != null) {
                currentCode = country[1];
                tvCountry.setText(country[0]);
            }

            tvCountryNum = (TextView) activity.findViewById(Res.id.tv_country_num);
            tvCountryNum.setText("+" + currentCode);

            etPhoneNum = (EditText) activity.findViewById(Res.id.et_write_phone);
            etPhoneNum.setText("");
            etPhoneNum.addTextChangedListener(this);
            etPhoneNum.requestFocus();
            if (etPhoneNum.getText().length() > 0) {
                btnNext.setEnabled(true);

                ivClear = (ImageView) activity.findViewById(Res.id.iv_clear);
                ivClear.setVisibility(View.VISIBLE);
                resId = getBitmapRes(activity, "smssdk_btn_enable");
                if (resId > 0) {
                    btnNext.setBackgroundResource(resId);
                }
            }

            ivClear = (ImageView) activity.findViewById(Res.id.iv_clear);

            llBack.setOnClickListener(this);
            btnNext.setOnClickListener(this);
            ivClear.setOnClickListener(this);
            viewCountry.setOnClickListener(this);

            handler = new EventHandler() {
                public void afterEvent(final int event, final int result,
                        final Object data) {
                    runOnUIThread(new Runnable() {
                        public void run() {
                            if (pd != null && pd.isShowing()) {
                                pd.dismiss();
                            }
                            if (result == SMSSDK.RESULT_COMPLETE) {
                                if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                                    // 请求验证码后,跳转到验证码填写页面
                                    boolean smart = (Boolean)data;
                                    afterVerificationCodeRequested(smart);
                                }
                            } else {
                                if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE
                                        && data != null
                                        && (data instanceof UserInterruptException)) {
                                    // 由于此处是开发者自己决定要中断发送的,因此什么都不用做
                                    return;
                                }

                                int status = 0;
                                // 根据服务器返回的网络错误,给toast提示
                                try {
                                    ((Throwable) data).printStackTrace();
                                    Throwable throwable = (Throwable) data;

                                    JSONObject object = new JSONObject(
                                            throwable.getMessage());
                                    String des = object.optString("detail");
                                    status = object.optInt("status");
                                    if (!TextUtils.isEmpty(des)) {
                                        Toast.makeText(activity, des, Toast.LENGTH_SHORT).show();
                                        return;
                                    }
                                } catch (Exception e) {
                                    SMSLog.getInstance().w(e);
                                }
                                // 如果木有找到资源,默认提示
                                int resId = 0;
                                if(status >= 400) {
                                    resId = getStringRes(activity,
                                            "smssdk_error_desc_"+status);
                                } else {
                                    resId = getStringRes(activity,
                                            "smssdk_network_error");
                                }

                                if (resId > 0) {
                                    Toast.makeText(activity, resId, Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    });
                }
            };
        }

    }

    private String[] getCurrentCountry() {
        String mcc = getMCC();
        String[] country = null;
        if (!TextUtils.isEmpty(mcc)) {
            country = SMSSDK.getCountryByMCC(mcc);
        }

        if (country == null) {
            Log.w("SMSSDK", "no country found by MCC: " + mcc);
            country = SMSSDK.getCountry(DEFAULT_COUNTRY_ID);
        }
        return country;
    }

    private String getMCC() {
        TelephonyManager tm = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
        // 返回当前手机注册的网络运营商所在国家的MCC+MNC. 如果没注册到网络就为空.
        String networkOperator = tm.getNetworkOperator();
        if (!TextUtils.isEmpty(networkOperator)) {
            return networkOperator;
        }

        // 返回SIM卡运营商所在国家的MCC+MNC. 5位或6位. 如果没有SIM卡返回空
        return tm.getSimOperator();
    }

    public void onResume() {
        SMSSDK.registerEventHandler(handler);
    }

    public void onPause() {
        SMSSDK.unregisterEventHandler(handler);
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.length() > 0) {
            btnNext.setEnabled(true);
            ivClear.setVisibility(View.VISIBLE);
            int resId = getBitmapRes(activity, "smssdk_btn_enable");
            if (resId > 0) {
                btnNext.setBackgroundResource(resId);
            }
        } else {
            btnNext.setEnabled(false);
            ivClear.setVisibility(View.GONE);
            int resId = getBitmapRes(activity, "smssdk_btn_disenable");
            if (resId > 0) {
                btnNext.setBackgroundResource(resId);
            }
        }
    }

    public void afterTextChanged(Editable s) {

    }

    public void onClick(View v) {
        int id = v.getId();
        int id_ll_back = Res.id.ll_back;
        int id_rl_country = Res.id.rl_country;
        int id_btn_next = Res.id.btn_next;
        int id_iv_clear = Res.id.iv_clear;

        if (id == id_ll_back) {
            finish();
        } else if (id == id_rl_country) {
            // 国家列表
            CountryPage countryPage = new CountryPage();
            countryPage.setCountryId(currentId);
            countryPage.showForResult(activity, null, this);
        } else if (id == id_btn_next) {
            // 请求发送短信验证码
            String phone = etPhoneNum.getText().toString().trim().replaceAll("\\s*", "");
            String code = tvCountryNum.getText().toString().trim();
            showDialog(phone, code);
        } else if (id == id_iv_clear) {
            // 清除电话号码输入框
            etPhoneNum.getText().clear();
        }
    }

    @SuppressWarnings("unchecked")
    public void onResult(HashMap<String, Object> data) {
        if (data != null) {
            int page = (Integer) data.get("page");
            if (page == 1) {
                // 国家列表返回
                currentId = (String) data.get("id");
                String[] country = SMSSDK.getCountry(currentId);
                if (country != null) {
                    currentCode = country[1];
                    tvCountryNum.setText("+" + currentCode);
                    tvCountry.setText(country[0]);
                }
            } else if (page == 2) {
                // 验证码校验返回
                Object res = data.get("res");
                //Object smart = data.get("smart");

                HashMap<String, Object> phoneMap = (HashMap<String, Object>) data.get("phone");
                if (res != null && phoneMap != null) {
                    int resId = getStringRes(activity, "smssdk_your_ccount_is_verified");
                    if (resId > 0) {
                        Toast.makeText(activity, resId, Toast.LENGTH_SHORT).show();
                    }

                    if (callback != null) {
                        callback.afterEvent(
                                SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE,
                                SMSSDK.RESULT_COMPLETE, phoneMap);
                    }
                    finish();
                }
            }
        }
    }

    /** 分割电话号码 */
    private String splitPhoneNum(String phone) {
        StringBuilder builder = new StringBuilder(phone);
        builder.reverse();
        for (int i = 4, len = builder.length(); i < len; i += 5) {
            builder.insert(i, ' ');
        }
        builder.reverse();
        return builder.toString();
    }

    /** 是否请求发送验证码,对话框 */
    public void showDialog(final String phone, final String code) {
        int resId = getStyleRes(activity, "CommonDialog");
        if (resId > 0) {
            final String phoneNum = code + " " + splitPhoneNum(phone);
            final Dialog dialog = new Dialog(getContext(), resId);

            LinearLayout layout = SendMsgDialogLayout.create(getContext());

            if (layout != null) {
                dialog.setContentView(layout);

                ((TextView) dialog.findViewById(Res.id.tv_phone)).setText(phoneNum);
                TextView tv = (TextView) dialog.findViewById(Res.id.tv_dialog_hint);
                resId = getStringRes(activity, "smssdk_make_sure_mobile_detail");
                if (resId > 0) {
                    String text = getContext().getString(resId);

                    tv.setText(Html.fromHtml(text));
                }

                ((Button) dialog.findViewById(Res.id.btn_dialog_ok)).setOnClickListener(
                        new OnClickListener() {
                                public void onClick(View v) {
                                    // 跳转到验证码页面
                                    dialog.dismiss();

                                    if (pd != null && pd.isShowing()) {
                                        pd.dismiss();
                                    }
                                    pd = CommonDialog.ProgressDialog(activity);
                                    if (pd != null) {
                                        pd.show();
                                    }
                                    Log.e("verification phone ==>>", phone);
                                    SMSSDK.getVerificationCode(code, phone.trim(), osmHandler);
                                }
                            });


                    ((Button) dialog.findViewById(Res.id.btn_dialog_cancel)).setOnClickListener(
                            new OnClickListener() {
                                public void onClick(View v) {
                                    dialog.dismiss();
                                }
                            });
                dialog.setCanceledOnTouchOutside(true);
                dialog.show();
            }
        }
    }

    /** 请求验证码后,跳转到验证码填写页面 */
    private void afterVerificationCodeRequested(boolean smart) {
        String phone = etPhoneNum.getText().toString().trim().replaceAll("\\s*", "");
        String code = tvCountryNum.getText().toString().trim();
        if (code.startsWith("+")) {
            code = code.substring(1);
        }
        String formatedPhone = "+" + code + " " + splitPhoneNum(phone);

        // 验证码页面
        if(smart) {
            SmartVerifyPage smartPage = new SmartVerifyPage();
            smartPage.setPhone(phone, code, formatedPhone);
            smartPage.showForResult(activity, null, this);
        } else {
            IdentifyNumPage page = new IdentifyNumPage();
            page.setPhone(phone, code, formatedPhone);
            page.showForResult(activity, null, this);
        }
    }

}

然后代码实现

// 打开注册页面
            RegisterPage registerPage = new 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;
                        country = (String) phoneMap.get("country");
                        phone = (String) phoneMap.get("phone");
                        SharedPreferences sha = getSharedPreferences("phone",
                                MODE_PRIVATE);
                        Editor editor = sha.edit();
                        editor.putString("name", phone);
                        editor.putString("pwss", country);
                        editor.commit();
                        et_phone.setText(phone);
                        paws.setText(country);
                    }
                }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值