MobSMSSDK

作者: 丶Mars绝版 | 来源:发表于2018-01-06 09:30 被阅读0次
    image.png

    加入以下权限

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    
    <uses-permission android:name="android.permission.INTERNET" />
    
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    
    <uses-permission android:name="android.permission.GET_TASKS" />
    
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    

    初始化SDK,使用前必须初始化,所以这里直接在application里面进行初始化
    记得在AndroidManifest.xml

    
    public class MyApplication extends MobApplication {
        @Override
        public void onCreate() {
            super.onCreate();
            //在使用SDK各组件之前初始化context信息,传入ApplicationContext
            //注意该方法要再setContentView方法之前实现
            MobSDK.init(getApplicationContext(),"您的appkey", "您的appsecret");
        }
    
    }
    

    获取验证码的代码

      //用户点击获取验证码
        @OnClick(R.id.get_code_tv)
        public void onGetCodeClick(TextView getVerificationCode) {
            //用户号码
            String userPhoneNumb = mUserName.getText().toString();
            if (StringUtil.isChinaPhoneLegal(userPhoneNumb) || userPhoneNumb.length() != 11) {
                Snackbar.make(mActivitySplash, "请输入正确的手机号", Snackbar.LENGTH_SHORT).show();
                return;
            }
            mGetCodeTv.setClickable(false);
            //定时器
            if(mTimer==null) {
                mTimer = new Timer();
            }
            TimerTask task = new TimerTask() {
                @Override
                public void run() {
                    // 需要做的事:发送消息
                    runOnUiThread(new Runnable() {
                        public int clock = 60;
    
                        @Override
                        public void run() {
                            if (clock < 0) {
                                clock = 60;
                                mGetCodeTv.setClickable(true);
                                mGetCodeTv.setText("重新获取");
                                mTimer.cancel();
                                mTimer = null;
                                return;
                            }
                            mGetCodeTv.setText("重新获取"+"("+String.valueOf(clock--)+")");
                        }
                    });
                }
            };
            mTimer.schedule(task, 1000, 1000); // 1s后执行task,经过1s再次执行
            // 创建EventHandler对象
            eventHandler = new EventHandler() {
                public void afterEvent(int event, int result, Object data) {
                    if (result == SMSSDK.RESULT_ERROR) {
                        //验证失败
                        //错误码请参照http://wiki.mob.com/android-api-错误码参考/这里我就不再继续写了
                        String message = ((Throwable) data).getMessage();
                        ResultBean resultBean = (ResultBean) JsonUtil.JsonStr2Bean(message, ResultBean.class);
                        Snackbar.make(mActivitySplash, resultBean.detail, Snackbar.LENGTH_SHORT).show();
                    }
                    if (result == SMSSDK.RESULT_COMPLETE) {
                        //回调完成
                        if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                            //提交验证码成功(验证成功)
                            Snackbar.make(mActivitySplash, "提交验证码成功", Snackbar.LENGTH_SHORT).show();
                        } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                            //获取验证码成功
                            Snackbar.make(mActivitySplash, "获取验证码成功", Snackbar.LENGTH_SHORT).show();
                        }
                    } else {
                        ((Throwable) data).printStackTrace();
                    }
                }
            };
            // 注册监听器
            SMSSDK.registerEventHandler(eventHandler);
            //获取验证码
            SMSSDK.getVerificationCode("86", userPhoneNumb);
        }
    
    

    确认注册,进行验证

     //用户点击确认修改
        @OnClick(R.id.regist_btn)
        public void onSureRegistClick(Button sureRegist) {
            if(!Cons.NETWORK) {
                Snackbar.make(mActivitySplash, "网络异常", Snackbar.LENGTH_SHORT).show();
                return;
            }
            String userPhoneNumb = mUserName.getText().toString();
            String userPwd = mPassword.getText().toString();
            String verificationCode = mVerificationCode.getText().toString();
            if (TextUtils.isEmpty(userPhoneNumb) || TextUtils.isEmpty(userPwd)) {
                Snackbar.make(mActivitySplash, "帐号或密码为空", Snackbar.LENGTH_SHORT).show();
                return;
            }
            if (StringUtil.isChinaPhoneLegal(userPhoneNumb) || userPhoneNumb.length() != 11) {
                Snackbar.make(mActivitySplash, "请输入正确的手机号", Snackbar.LENGTH_SHORT).show();
                return;
            }
            if (TextUtils.isEmpty(verificationCode)) {
                Snackbar.make(mActivitySplash, "验证码为空", Snackbar.LENGTH_SHORT).show();
                return;
            }
            if (userPwd.length()<6||userPwd.length()>16) {
                Snackbar.make(mActivitySplash, "密码长度不正确", Snackbar.LENGTH_SHORT).show();
                return;
            }
            SMSSDK.submitVerificationCode("86", userPhoneNumb, mVerificationCode.getText()
                    .toString());
        }
    

    在销毁的时候记得销毁

    protected void onDestroy() {
            super.onDestroy();
            SMSSDK.unregisterEventHandler(eventHandler);
        }
    

    相关文章

      网友评论

          本文标题:MobSMSSDK

          本文链接:https://www.haomeiwen.com/subject/isnenxtx.html