shareSDK短信验证

用户登录

短信验证步骤

用户登录模块采用短信验证的方式,登录用户,所以采用sharesdk进行短信验证

查看sharesdk文档需要做如下操作

步骤一:下载sharesdk短信验证相关jar包和demo,将jar包中aar文件拷贝至工程libs目录下
        SMSSDK-.aar          SMSSDK 核心         必须
        SMSSDKGUI-.aar       SMSSDK GUI 开源库   可选

      添加如下代码,让上诉2个aar文件可以在工程中使用
        repositories{
            flatDir{
                dirs 'libs' //就是你放aar的目录地址
            }
        }

        compile name: 'SMSSDK-2.1.3', ext: 'aar'
        compile name: 'SMSSDKGUI-2.1.3', ext: 'aar'
 (这里需要注意的是你下在的那个版本号的,就把2.1.3换成我们的版本号)
步骤二:导入jar包,将jar包添加至工作目录
        compile files('libs/MobCommons-2016.1201.1839.jar')
        compile files('libs/MobTools-2016.1201.1839.jar')
步骤三:添加sharesdk需要用到权限
    
    
    
    
    
    
    
    
    
    
步骤四:权限校验: SMSUtil.checkPermission(this);
步骤五:初始化工具: SMSSDK.initSDK(this, APPKEY, APPSECRET, true);
步骤六:注册事件监听:SMSSDK.registerEventHandler(eventHandler);
步骤七:获取验证码:SMSSDK.getVerificationCode("86", phone);监听事件触发。
步骤八:发送验证码:SMSSDK.submitVerificationCode("86", phone, code.trim());监听事件触发。
步骤九:注销监听:
    @Override
    protected void onDestroy() {
        //取消监听
        SMSSDK.unregisterAllEventHandler();
        super.onDestroy();
    }

1 短信注册获取验证码

1.1 配置初始化信息

//配置key和Secret
SMSSDK.initSDK(this, "18dbd22d4813c", "46d6cf40cfcab12e05af5adf2484a0c5",true);
//监听
SMSSDK.registerEventHandler(eventHandler);

1.2 注册按钮点击事件

 @OnClick({R.id.iv_user_back, R.id.login, R.id.tv_user_code})
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.iv_user_back:
            break;
        case R.id.tv_user_code:
            //点击获取验证码按钮
            getVerificationCode();
            break;
        case R.id.login:
            //获取电话号码
            String phone = etUserPhone.getText().toString();
            boolean isPhoneNumber = SMSUtil.judgePhoneNums(this, phone);

            //获取验证码
            String code = etUserCode.getText().toString();
            if (isPhoneNumber && !TextUtils.isEmpty(code)){
                //电话号码和验证码都是可用的状态
                SMSSDK.submitVerificationCode("86",phone,code);
            }
            break;
    }
}
//获取验证码,毫秒值计数
private void getVerificationCode() {
    time = 60;
    //电话号码,判断电话号码是否为空
    String phone = etUserPhone.getText().toString();
    boolean isPhoneNumber = SMSUtil.judgePhoneNums(this, phone);
    if (isPhoneNumber){
        //如果输入电话号码无误,则可以对此号码发送验证码
        SMSSDK.getVerificationCode("86",phone,new OnSendMessageHandler(){
            @Override
            public boolean onSendMessage(String s, String s1) {
                return false;
            }
        });
        tvUserCode.setEnabled(false);
        //开启子线程,开始倒计时
        new Thread(){
            @Override
            public void run() {
                while (time>0){
                    handler.sendEmptyMessage(KEEP_COUNTING);
                    try {
                        Thread.sleep(999);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                handler.sendEmptyMessage(RESET_TIME);
            }
        }.start();
    }
}

1.3 监听验证码是否发送成功

private EventHandler eventHandler = new EventHandler(){
    @Override
    public void afterEvent(int event, int result, Object data) {
        if (result == SMSSDK.RESULT_COMPLETE){
            if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                Log.i(TAG,"验证:"+Thread.currentThread().getId());
                handler.sendEmptyMessage(GET_VERIFICATION_CODE_SUCCESS);
                //如果事件是发送了验证码短信,获取短信验证码后可以进行短信验证
            }
            ......
        }else{
            if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                //如果事件是发送了验证码短信,获取短信验证码后可以进行短信验证
                handler.sendEmptyMessage(GET_VERIFICATION_CODE_FAIL);
            }
            ......
        }
        super.afterEvent(event, result, data);
    }
};

private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
            case KEEP_COUNTING:
                //每隔一秒时间往下减少
                tvUserCode.setText("(稍后重发"+(--time)+")");
                break;
            case RESET_TIME:
                //时间重置为一分钟
                time = 60;
                tvUserCode.setEnabled(true);
                tvUserCode.setText("重新发送验证码");
                break;
            case GET_VERIFICATION_CODE_SUCCESS:
                Toast.makeText(LoginActivity.this,"验证码已发送",Toast.LENGTH_SHORT).show();
                break;
            case GET_VERIFICATION_CODE_FAIL:
                Toast.makeText(LoginActivity.this,"验证码发送失败",Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

1.4 验证码是否匹配

给指定手机发送的验证码和提交的验证码是否一致,如果一致,则说明可以开始注册了

@OnClick({R.id.iv_user_back, R.id.login, R.id.tv_user_code})
public void onClick(View view) {
    switch (view.getId()) {
        ......
        case R.id.login:
            //获取电话号码
            String phone = etUserPhone.getText().toString();
            boolean isPhoneNumber = SMSUtil.judgePhoneNums(this, phone);

            //获取验证码
            String code = etUserCode.getText().toString();
            if (isPhoneNumber && !TextUtils.isEmpty(code)){
                //电话号码和验证码都是可用的状态
                SMSSDK.submitVerificationCode("86",phone,code);
            }
            break;
    }
}

private EventHandler eventHandler = new EventHandler(){
    @Override
    public void afterEvent(int event, int result, Object data) {
        if (result == SMSSDK.RESULT_COMPLETE){
            if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                Log.i(TAG,"验证:"+Thread.currentThread().getId());
                handler.sendEmptyMessage(GET_VERIFICATION_CODE_SUCCESS);
                //如果事件是发送了验证码短信,获取短信验证码后可以进行短信验证
            }else if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE){
                handler.sendEmptyMessage(SUBMIT_VERIFICATION_CODE_SUCCESS);
            }
        }else{
            if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                //如果事件是发送了验证码短信,获取短信验证码后可以进行短信验证
                handler.sendEmptyMessage(GET_VERIFICATION_CODE_FAIL);
            }else{
                handler.sendEmptyMessage(SUBMIT_VERIFICATION_CODE_FAIL);
            }
        }
        super.afterEvent(event, result, data);
    }
};

 private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
            ......
            case SUBMIT_VERIFICATION_CODE_SUCCESS:
                //如果事件是发送了验证码,判断是否验证成功
                //成功,注册成功
                Toast.makeText(LoginActivity.this,"可以注册了",Toast.LENGTH_SHORT).show();
                break;
            ......
            case SUBMIT_VERIFICATION_CODE_FAIL:
                //如果事件是发送了验证码,判断是否验证成功
                //成功,注册成功
                Toast.makeText(LoginActivity.this,"验证码验证不通过",Toast.LENGTH_SHORT).show();
                break;
        }
    }

2 用户注册

发送网络请求,注册用户一旦注册成功,需要将服务器的数据插入或者同步到本地的用户数据库

private void registerUser() {
    String phone = etUserPhone.getText().toString();
    String psd = etUserPsd.getText().toString();
    //注册用户,电话号码做为账号,验证码作为密码登陆
    LoginPresenter loginPresenter = new LoginPresenter(this);
    loginPresenter.getLoginInfo(phone,psd,1);
}

public interface ResponseInfoAPI {
   ......
   //发送login请求,上次给服务器账号密码和类型
   @GET(Constant.LOGIN)
   Call getLoginInfo(@Query("username")String name,
                                   @Query("password")String psd,
                                   @Query("type") int type);
}

//注册请求告知服务器,服务器返回数据解析
public class LoginPresenter extends BasePresenter{
    private LoginActivity loginActivity;

    public LoginPresenter(LoginActivity loginActivity) {
        this.loginActivity = loginActivity;
    }

    @Override
    protected void showErrorMessage(String errorMessage) {
    }
    @Override
    protected void parseJson(String data) {
        Gson gson = new Gson();
        //获取
        UserInfo userInfo = gson.fromJson(data, UserInfo.class);
        //指定用户id
        MyApplication.userId = userInfo.getId();

        //用户登陆相关逻辑
        userLogin(userInfo);
        
        //结束actiivty
        loginActivity.finish();
    }

    public void getLoginInfo(String name,String psd,int type) {
        Call loginInfo = responseInfoAPI.getLoginInfo(name, psd, type);
        loginInfo.enqueue(new RequestCallBack());
    }
}

2.1 数据库操作

数据库操作使用

进行增删改查

compile 'com.j256.ormlite:ormlite-android:5.0'

创建项目数据库takeout.db,创建用户信息表

public class DBHelper extends OrmLiteSqliteOpenHelper {
    private static DBHelper dbHelper;
    private HashMap hashMap = new HashMap<>();
    private DBHelper(Context context) {
        super(context, "takeout.db", null, 1);
    }
    /**
     * @param ctx   上下文环境
     * @return  dbhelper对象
     */
    public static synchronized  DBHelper getInstance(Context ctx){
        if (dbHelper == null){
            dbHelper = new DBHelper(ctx);
        }
        return dbHelper;
    }
    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        try {
            //创建数据库
            TableUtils.createTable(connectionSource, UserInfo.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
    }
    /**
     * @param clazz 类的字节码文件
     * @return  字节码文件对于的dao对象,用于操作数据库
     */
    public Dao getDao(Class clazz){
        Dao dao = null;
        String simpleName = clazz.getSimpleName();
        dao = hashMap.get(simpleName);
        if (dao == null){
            try {
                //存储相应对象
                dao = super.getDao(clazz);
                hashMap.put(simpleName,dao);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return dao;
    }

    @Override
    public void close() {
        for (String daoSimpleName: hashMap.keySet()) {
            //将每一个daoSimpleName,至为空
            Dao dao = hashMap.get(daoSimpleName);
            dao = null;
        }
    }
}

2.2 用户信息javabean

@DatabaseTable(tableName = "t_user")
public class UserInfo {
    @DatabaseField(id = true)
    private int _id;

    @DatabaseField()
    private float balance;

    @DatabaseField()
    private float discount;

    @DatabaseField()
    private int integral;

    @DatabaseField()
    private String name;

    @DatabaseField()
    private String phone;

    @DatabaseField()
    private boolean isLogin;

    .......
}

2.3 用户登陆与数据库相关逻辑

private void userLogin(UserInfo userInfo) {
    androidDatabaseConnection = new AndroidDatabaseConnection
            (dbHelper.getWritableDatabase(), true);
    dao = dbHelper.getDao(UserInfo.class);

    try {
        //设置数据库操作回滚点
        start = androidDatabaseConnection.setSavePoint("start");
        //设置自动提交为否
        dao.setAutoCommit(androidDatabaseConnection,false);
        //查询所有用户
        List userInfoList = dao.queryForAll();
        if (userInfoList!=null && userInfoList.size()>0){
            //之前有用户登录过,存储了相关数据,将所有数据的登录状态设置为false
            for (int i = 0; i < userInfoList.size(); i++) {
                UserInfo user = userInfoList.get(i);
                //修改数据库中的登录状态
                user.setLogin(0);
                //将修改后的状态更新到数据库中
                dao.update(user);
            }
        }
        //将指定id的用户登录状态设置为true
        UserInfo userBean = dao.queryForId(userInfo.get_id());
        if (userBean!=null){
            //此用户之前登录过,只需要修改其登录状态即可
            userBean.setLogin(1);
            dao.update(userBean);
        }else{
            //此用户之前没有登录过,则需要插入一条新的数据,并且修改器状态
            UserInfo userLogin = new UserInfo();
            userLogin.setLogin(1);
            userLogin.set_id(userInfo.get_id());
            userLogin.setBalance(userInfo.getBalance());
            userLogin.setDiscount(userInfo.getDiscount());
            userLogin.setIntegral(userInfo.getIntegral());
            userLogin.setPhone(userInfo.getPhone());
            userLogin.setName(userInfo.getName());
            dao.create(userLogin);
        }
        //提交事物
        androidDatabaseConnection.commit(start);
        //更新UI效果
    } catch (SQLException e) {
        e.printStackTrace();
        if (androidDatabaseConnection!=null){
            try {
                //如果出现异常,则回滚到初始位置
                androidDatabaseConnection.rollback(start);
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    }
}

你可能感兴趣的:(shareSDK短信验证)