mob自定义短信验证

来源:互联网 发布:古剑奇谭 online 知乎 编辑:程序博客网 时间:2024/05/19 13:22

mob官网操作



添加应用之后






查看Appkey和App Sercret




SDK下载  、



接下来查看Android文档

http://wiki.mob.com/sdk-sms-android-3-0-0/  这是链接


创建跳转的类

import android.content.Intent;import android.os.Bundle;import android.os.CountDownTimer;import android.text.method.HideReturnsTransformationMethod;import android.text.method.PasswordTransformationMethod;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.EditText;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import com.sunhaitang.app_demo.R;import java.util.regex.Matcher;import java.util.regex.Pattern;import butterknife.BindView;import butterknife.ButterKnife;import butterknife.OnClick;import cn.smssdk.EventHandler;import cn.smssdk.SMSSDK;import me.imid.swipebacklayout.lib.app.SwipeBackActivity;import static android.widget.Toast.makeText;public class RegisterActivity extends SwipeBackActivity {    @BindView(R.id.z_zc)    ImageView mZZc;    @BindView(R.id.textView)    TextView mTextView;    @BindView(R.id.zc_name)    EditText mZcName;    @BindView(R.id.hqyzm)    TextView mHqyzm;    @BindView(R.id.zc_yzm)    EditText mZcYzm;    @BindView(R.id.zc_pwd)    EditText mZcPwd;    @BindView(R.id.zc_btn)    Button mZcBtn;    public EventHandler eh; //事件接收器    @BindView(R.id.cb_login)    CheckBox mCbLogin;    private TimeCount mTimeCount;//计时器    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_register);        ButterKnife.bind(this);        initView();        initData();    }    public void initView() {        mTimeCount = new TimeCount(60000, 1000);    }    public void initData() {        /**         * 显示隐藏密码         */        mCbLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {                if (b) {                    //如果选中,显示密码                    mZcPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());                } else {                    //否则隐藏密码                    mZcPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());                }            }        });    }    @OnClick({R.id.z_zc, R.id.hqyzm, R.id.zc_btn})    public void onViewClicked(View view) {        switch (view.getId()) {            case R.id.z_zc:                finish();                break;            case R.id.hqyzm:                /**                 * 获取短信验证码   = =!                 */                eh = new EventHandler() {                    @Override                    public void afterEvent(int event, int result, Object data) {                        if (result == SMSSDK.RESULT_COMPLETE) { //回调完成                            if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { //提交验证码成功                            } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { //获取验证码成功                            } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) { //返回支持发送验证码的国家列表                            }                        } else {                            ((Throwable) data).printStackTrace();                        }                    }                };                SMSSDK.registerEventHandler(eh); //注册短信回调                if (!mZcName.getText().toString().trim().equals("")) {                    if (checkTel(mZcName.getText().toString().trim())) {                        SMSSDK.getVerificationCode("+86", mZcName.getText().toString());//获取验证码                        mTimeCount.start();                    } else {                        makeText(RegisterActivity.this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();                    }                } else {                    makeText(RegisterActivity.this, "请输入手机号码", Toast.LENGTH_SHORT).show();                }                break;            case R.id.zc_btn:                if (!mZcName.getText().toString().trim().equals("")) {                    if (checkTel(mZcName.getText().toString().trim())) {                        if (!mZcYzm.getText().toString().trim().equals("")) {                            SMSSDK.submitVerificationCode("+86", mZcName.getText().toString().trim(), mZcYzm.getText().toString().trim());//提交验证                            if (!mZcPwd.getText().toString().trim().equals("")) {                                //注册完成 跳转                                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);                                startActivity(intent);                                mTimeCount.cancel();                                finish();                            } else {                                Toast toast = makeText(RegisterActivity.this, "密码格式有误", Toast.LENGTH_SHORT);                            }                        } else {                            Toast toast = makeText(RegisterActivity.this, "请输入验证码", Toast.LENGTH_SHORT);                        }                    } else {                        Toast toast = makeText(RegisterActivity.this, "请输入正确的手机号码", Toast.LENGTH_SHORT);                    }                } else {                    Toast toast = makeText(RegisterActivity.this, "请输入手机号码", Toast.LENGTH_SHORT);                }                break;        }    }    /**     * 计时器     */    class TimeCount extends CountDownTimer {        public TimeCount(long millisInFuture, long countDownInterval) {            super(millisInFuture, countDownInterval);        }        @Override        public void onTick(long l) {            mHqyzm.setClickable(false);            mHqyzm.setText(l / 1000 + "秒后重新获取");        }        @Override        public void onFinish() {            mHqyzm.setClickable(true);            mHqyzm.setText("获取验证码");        }    }    /**     * 正则匹配手机号码     *     * @param tel     * @return     */    public boolean checkTel(String tel) {        Pattern p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$");        Matcher matcher = p.matcher(tel);        return matcher.matches();    }    @Override    protected void onDestroy() {        super.onDestroy();        SMSSDK.unregisterEventHandler(eh);    }}
xml文件复制进去就完美了


<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.sunhaitang.app_demo.view.RegisterActivity">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="15dp"        android:layout_marginRight="15dp"        android:layout_marginTop="10dp">        <ImageView            android:id="@+id/z_zc"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@mipmap/cha"/>        <TextView            android:id="@+id/textView"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center"            android:text="手机快速注册"            android:textSize="20sp"/>    </RelativeLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="40dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="15dp"            android:layout_marginTop="10dp"            android:text="账号"            android:textSize="18sp"/>        <View            android:layout_width="match_parent"            android:layout_height="1dp"            android:layout_marginLeft="15dp"            android:layout_marginRight="15dp"            android:layout_marginTop="34dp"            android:background="#bdbdc1"/>        <EditText            android:id="@+id/zc_name"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginLeft="100dp"            android:layout_marginTop="10dp"            android:background="@null"            android:hint="用户名/邮箱/手机号"            android:textCursorDrawable="@null"/>    </RelativeLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="40dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="15dp"            android:layout_marginTop="10dp"            android:text="验证码"            android:textSize="18sp"/>        <TextView            android:id="@+id/hqyzm"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginRight="15dp"            android:layout_marginTop="10dp"            android:gravity="right"            android:text="获取验证码"            android:textColor="#f00"/>        <View            android:layout_width="match_parent"            android:layout_height="1dp"            android:layout_marginLeft="15dp"            android:layout_marginRight="115dp"            android:layout_marginTop="34dp"            android:background="#bdbdc1"/>        <EditText            android:id="@+id/zc_yzm"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginLeft="100dp"            android:layout_marginRight="140dp"            android:layout_marginTop="10dp"            android:background="@null"            android:hint="请输入验证码"            android:maxLength="4"            android:textCursorDrawable="@null"/>    </RelativeLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="40dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="15dp"            android:layout_marginTop="10dp"            android:text="密码"            android:textSize="18sp"/>        <View            android:layout_width="match_parent"            android:layout_height="1dp"            android:layout_marginLeft="15dp"            android:layout_marginRight="15dp"            android:layout_marginTop="34dp"            android:background="#bdbdc1"/>        <CheckBox            android:id="@+id/cb_login"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:layout_marginRight="15dp"            android:layout_marginTop="3dp"            android:background="#ccc"            android:button="@null"/>        <EditText            android:id="@+id/zc_pwd"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginLeft="100dp"            android:layout_marginRight="50dp"            android:layout_marginTop="10dp"            android:background="@null"            android:hint="请输入密码"            android:password="true"            android:textCursorDrawable="@null"/>    </RelativeLayout>    <Button        android:id="@+id/zc_btn"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="15dp"        android:layout_marginRight="15dp"        android:layout_marginTop="50dp"        android:background="#ccc"        android:text="注册"/></LinearLayout>



效果图为: