Android中短信验证的功能实现

多用于手机客户端注册与忘记密码等与短信验证相关的模块


需要先进行手机号验证,再通过接口接受数据

布局页面.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FDFDFE"
    android:orientation="vertical"
    tools:context="com.fangshuoit.kuaikao.activity.ForgetActivity">

    <include layout="@layout/base_title" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/img_scenery"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="fitXY"
            android:src="@mipmap/shiyan" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:paddingBottom="5dp"
            android:paddingTop="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:text="手  机  号:"
                android:textColor="@color/grayer"
                android:textSize="15sp" />

            <EditText
                android:id="@+id/et_user_phone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                android:background="@drawable/edittext_shape_small"
                android:hint="请输入手机号"
                android:paddingBottom="8dp"
                android:paddingTop="8dp"
                android:textSize="15sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:paddingBottom="5dp"
            android:paddingTop="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:text="验  证  码:"
                android:textColor="@color/grayer"
                android:textSize="15sp" />

            <EditText
                android:id="@+id/et_verification"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/edittext_shape_small"
                android:hint="请输入验证码"
                android:paddingBottom="8dp"
                android:paddingTop="8dp"
                android:textSize="15sp" />

            <Button
                android:id="@+id/btn_verificationcode"
                android:layout_width="wrap_content"
                android:layout_height="34dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="20dp"
                android:background="@drawable/button_shape"
                android:text="获取验证码"
                android:textColor="@color/light_white"
                android:textSize="12sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:orientation="horizontal"
            android:paddingBottom="5dp"
            android:paddingTop="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:text="新  密  码:"
                android:textColor="@color/grayer"
                android:textSize="15sp" />

            <EditText
                android:id="@+id/et_newPwd"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                android:background="@drawable/edittext_shape_small"
                android:hint="请输入新密码"
                android:inputType="textPassword"
                android:paddingBottom="8dp"
                android:paddingTop="8dp"
                android:textSize="15sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingBottom="5dp"
            android:paddingTop="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:text="确认密码:"
                android:textColor="@color/grayer"
                android:textSize="15sp" />

            <EditText
                android:id="@+id/et_newPwd2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                android:background="@drawable/edittext_shape_small"
                android:hint="请确认密码"
                android:inputType="textPassword"
                android:paddingBottom="8dp"
                android:paddingTop="8dp"
                android:textSize="15sp" />
        </LinearLayout>

        <Button
            android:id="@+id/btn_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/button_shape"
            android:text="提交"
            android:textColor="@color/light_white"
            android:textSize="16sp" />
    </LinearLayout>
</LinearLayout>

主activity.java
package com.fangshuoit.kuaikao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.fangshuoit.kuaikao.R;
import com.fangshuoit.kuaikao.application.SysConstants;
import com.fangshuoit.kuaikao.utils.CountDownTimerUtils;
import com.fangshuoit.kuaikao.utils.FSParameter;
import com.fangshuoit.kuaikao.utils.JSONUtil;
import com.fangshuoit.kuaikao.utils.StringUtils;
import com.fangshuoit.kuaikao.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import okhttp3.Call;

public class ForgetActivity extends AppCompatActivity {
    private TextView baseTitle;
    private ImageView baseBack;
    private Button btn_submit, btn_verificationcode;
    private EditText et_newPwd, et_newPwd2, et_verification, et_user_phone;
    private String verification;
    private String newPassword;
    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);
        SMSSDK.initSDK(this, SysConstants.SMSSDK_APP_KEY, SysConstants.SMSSDK_APP_SECRET);
        EventHandler eh = new EventHandler() {
            @Override
            public void afterEvent(int event, int result, Object data) {
                Message msg = new Message();
                msg.arg1 = event;
                msg.arg2 = result;
                msg.obj = data;
                handler.sendMessage(msg);
            }
        };
        SMSSDK.registerEventHandler(eh);
        initView();
        initEvent();
    }

    private void initEvent() {
        Bundle bunde = this.getIntent().getExtras();
        String username = bunde.getString("username");
        et_user_phone.setText(username);

        baseBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        baseTitle.setText("忘记密码");
        btn_verificationcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phone = et_user_phone.getText().toString().trim();
                boolean judge = isMobileNO(phone);
                if (judge) {
                    //验证码
                    CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(btn_verificationcode, 60000, 1000);
                    mCountDownTimerUtils.start();
                    SMSSDK.getVerificationCode("86", phone);
                    et_verification.requestFocus();
                } else {
                    Toast.makeText(ForgetActivity.this, "手机号不合法,请重新输入!", Toast.LENGTH_SHORT).show();
                    et_user_phone.requestFocus();
                }
            }
        });
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 验证码验证
                verification = et_verification.getText().toString().trim();
                phone = et_user_phone.getText().toString().trim();
                newPassword = et_newPwd.getText().toString().trim();
                if (!isMobileNO(phone)) {
                    ToastUtils.show(ForgetActivity.this, "手机号错误!");
                    et_user_phone.requestFocus();
                } else if (StringUtils.isBlank(verification)) {
                    ToastUtils.show(ForgetActivity.this, "验证码不能为空!");
                    et_verification.requestFocus();
                } else if (newPassword.length() < 6) {
                    ToastUtils.show(ForgetActivity.this, "新密码不能少于6位!");
                    et_newPwd.requestFocus();
                } else if (newPassword.equals(et_newPwd2.getText().toString().trim())) {
                    ToastUtils.show(ForgetActivity.this, "新密码不一致,请重新输入!");
                    et_newPwd2.requestFocus();
                } else {
                    SMSSDK.submitVerificationCode("86", phone, verification);
                }
            }
        });
    }

    //验证手机格式
    public static boolean isMobileNO(String mobiles) {
        String telRegex = "[1][358]\\d{9}";
        if (TextUtils.isEmpty(mobiles)) return false;
        else return mobiles.matches(telRegex);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int event = msg.arg1;
            int result = msg.arg2;
            Object data = msg.obj;
            Log.e("event", "event=" + event);
            if (result == SMSSDK.RESULT_COMPLETE) {
                //短信注册成功后,返回LoginActivity进行登录
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {//提交验证码成功,验证通过
                    ToastUtils.show(getApplicationContext(), "验证码校验成功");
                    userCode();//用户验证码
                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {//服务器验证码发送成功
                    ToastUtils.show(getApplicationContext(), "验证码已经发送");
                } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {//返回支持发送验证码的国家列表
                    ToastUtils.show(getApplicationContext(), "获取列表成功");
                }
            } else {
                ToastUtils.show(ForgetActivity.this, "验证码获取失败,请重新获取!");
            }
        }
    };

    private void userCode() {
        String url = SysConstants.SERVER + SysConstants.FORGET_PASS;
        //调用工具类对参数进行封装
        FSParameter parameter = new FSParameter();
        parameter.putParameter("phone", phone);
        parameter.putParameter("newPassword", newPassword);
        //网络框架请求
        OkHttpUtils
                .postString()
                .url(url)
                .mediaType(parameter.getMediaType())
                .content(parameter.getParametersJsonString())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                        ToastUtils.show(ForgetActivity.this, "网络错误!");
                    }

                    @Override
                    public void onResponse(String response) {
                        String code = JSONUtil.getString(response, "code", "");
                        if (code.equals("0")) {
                            ToastUtils.show(ForgetActivity.this, "修改密码成功!");
                            Intent intent = new Intent();
                            int username = Integer.parseInt(et_user_phone.getText().toString());
                            intent.putExtra("username", username);
                            //通过intent对象返回结果,必须要调用一个setResult方法,
                            //setResult(resultCode, data);第一个参数表示结果返回码,一般只要大于1就可以,但是
                            setResult(2, intent);

                            finish(); //结束当前的activity的生命周期
                        } else {
                            ToastUtils.show(ForgetActivity.this, "修改失败!");
                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterAllEventHandler();
    }

    private void initView() {
        baseTitle = (TextView) findViewById(R.id.tv_title);
        baseBack = (ImageView) findViewById(R.id.iv_back);
        btn_submit = (Button) findViewById(R.id.btn_submit);
        btn_verificationcode = (Button) findViewById(R.id.btn_verificationcode);
        et_verification = (EditText) findViewById(R.id.et_verification);
        et_newPwd = (EditText) findViewById(R.id.et_newPwd);
        et_newPwd2 = (EditText) findViewById(R.id.et_newPwd2);
        et_user_phone = (EditText) findViewById(R.id.et_user_phone);
    }
}








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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值