情诗网 >情话短信 > 正文

Mob短信集成

来源:情诗网    2021-02-16    分类:情话短信

一、配置gradle

1、打开项目根目录的build.gradle,在buildscrip–>dependencies 模块下面添加 classpath ‘com.mob.sdk:MobSDK:+’,如下所示;(上方maven也是)

buildscript {
    repositories {
        jcenter()
        maven { url 'https://jitpack.io' }

        maven {
            url "http://mvn.mob.com/android"
        }

    }

    dependencies {
        ...
        classpath 'com.mob.sdk:MobSDK:+'

    }
}

2、在使用SMSSDK模块的build.gradle中,添加MobSDK插件和扩展,如:

// 添加插件

//系统自带的
apply plugin: 'com.android.application'
// 添加的插件
apply plugin: 'com.mob.sdk'

// 在MobSDK的扩展中注册SMSSDK的相关信息(和android同级,不要放在android里面)
MobSDK {
    appKey "d580ad56b4b5"
    appSecret "7fcae59a62342e7e2759e9e397c82bdd"

    SMSSDK {
            //默认使用GUI,若不使用GUI,通过以下开关关闭
            //gui false
            //若使用GUI的自动填充验证码功能,需打开此设置
            //autoSMS true     
           }
       }

二、添加代码

1、初始化MobSDK
如果您没有在AndroidManifest中设置appliaction的类名,MobSDK会将这个设置为com.mob.MobApplication,但如果您设置了,请在您自己的Application类中调用:

MobSDK.init (context, "2c32dbfd23b30", "1a073f14460c26285b8fbaa149ca528a");

2、Mainactivity中的oncreate()中第一行就得进行初始化

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        MobSDK.init (getActivity (), "2c32dbfd23b30", "1a073f14460c26285b8fbaa149ca528a");
        setContentView (R.layout.activity_main);
        initView ();
        initSMSSDK ();

    //上传手机号进行获取验证码
  mBtnHuoQu.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View v) {
                setEditText ();
                if (!mUser.getText ().toString ().isEmpty ()) {
                    SMSSDK.getVerificationCode ("86", registerPhone);
                    timer ();
                } else {
                    Toast.makeText (getActivity (), "请输入手机号", Toast.LENGTH_SHORT).show ();
                }
            }
        });

 private void timer() {

        timer = new CountDownTimer (61000, 100) {
            @Override
            public void onTick(long millisUntilFinished) {
                mBtnYanzheng.setEnabled (false);
                mBtnYanzheng.setText ("发送验证码" + "(" + millisUntilFinished / 1000 + ")");
                mBtnYanzheng.setBackgroundColor (Color.GRAY);

            }

            @Override
            public void onFinish() {
                mBtnYanzheng.setEnabled (true);
                mBtnYanzheng.setText ("发送验证码");
                codeResult = false;
                mBtnYanzheng.setBackgroundColor (getResources ().getColor (R.color.colorblue));
            }
        }.start ();

    }

  private void setEditText() {
        registerPhone = mUser.getText ().toString ();
        registerCode = mEtYanzheng.getText ().toString (); //获取输入的验证码
    }


  case R.id.btn_login:
                String user = mUser.getText ().toString ();
                String psw = mPassword.getText ().toString ();
                if (user.isEmpty () || psw.isEmpty ()) {
                    Toast.makeText (getActivity (), "注册信息不能为空", Toast.LENGTH_SHORT).show ();
                } else {

                    if (user.matches ("[a-zA-z1-9]{9,17}") || psw.matches ("[a-zA-Z1-9]{9,16}")) {
                       //获取手机号码和验证码
                        setEditText ();    
                        //提交验证码  在eventHandler里面查看验证结果  code为验证码
                        SMSSDK.submitVerificationCode ("86", registerPhone, registerCode);
                    } else {
                        Toast.makeText (getActivity (), "请输入正确的信息", Toast.LENGTH_SHORT).show ();
                    }
                }

  //进行回调等操作来进行验证码等逻辑处理
 private void initSMSSDK() {
        handler = new EventHandler () {
            @Override
            public void afterEvent(final int event, final int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功
                        getActivity ().runOnUiThread (new Runnable () {
                            @Override
                            public void run() {
                                Toast.makeText (getActivity (), "验证成功", Toast.LENGTH_SHORT).show ();
                                Intent intent = new Intent (getActivity (), HomeActivity.class);
                                startActivity (intent);
                                getActivity ().finish ();
                            }
                        });

                    } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        //获取验证码成功
                        getActivity ().runOnUiThread (new Runnable () {
                            @Override
                            public void run() {
                                Toast.makeText (getActivity (), "验证码已发送", Toast.LENGTH_SHORT).show ();
                            }
                        });
                    } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                        Toast.makeText (getActivity (), "验证码发送失败", Toast.LENGTH_SHORT).show ();
                    }
                } else {
                    ((Throwable) data).printStackTrace ();
                    Throwable throwable = (Throwable) data;
                    try {
                        //来对比验证码是否正确
                        JSONObject obj = new JSONObject (throwable.getMessage ());
                        final String des = obj.optString ("detail");
                        if (!TextUtils.isEmpty (des)) {
                            getActivity ().runOnUiThread (new Runnable () {
                                @Override
                                public void run() {
                                    Toast.makeText (getActivity (), "验证码不正确", Toast.LENGTH_SHORT).show ();
                                }
                            });
                        }
                    } catch (JSONException e) {
                        e.printStackTrace ();
                    }

                }
            }
        };
        //来通知handler操作
        SMSSDK.registerEventHandler (handler);
    }


    @Override
    public void onDestroy() {
        super.onDestroy ();
        //销毁
        SMSSDK.unregisterEventHandler (handler);
    }

热门文章