android短信验证码功能实现,包含按钮倒计时操作

来源:互联网 发布:js实现5秒倒计时 编辑:程序博客网 时间:2024/06/05 03:18

实习的时候,公司注册页面需要加上短信验证码的功能,就自己做了一个,使用的是聚合数据的短信验证SDK进行验证。

首先下载短信验证码SDK,不懂的朋友自行百度聚合数据,然后配置环境,导入相关的jar包.
这里写图片描述

2.进行mainfest的配置,加入权限

<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.ACCESS_FINE_LOCATION" />    <uses-permission android:name="android.permission.SEND_SMS" />

并且在application节点中配置meta,key为你申请的appkey

 <meta-data            android:name="JUHE_KEY"            android:value="key" />

页面布局:
这里写图片描述

布局代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="输入手机号:"/>    <EditText        android:layout_width="300dp"        android:layout_height="wrap_content"        android:textColor="#000000"        android:inputType="phone"        android:id="@+id/register_phone"/>    <Button        android:id="@+id/btn_register"        android:textColor="#ffffff"        android:layout_width="wrap_content"        android:layout_height="40dp"        android:padding="5dip"        android:background="@drawable/bg_btn_shape_login"        android:text="发送验证码"        android:textSize="16sp" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="输入验证码"/>    <EditText        android:layout_width="100dp"        android:layout_height="wrap_content"        android:id="@+id/register_yanzhengma"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="提交"        android:textColor="#000000"        android:id="@+id/btn_register_sumbit"/></LinearLayout>

图中的按钮为自定义样式,有需要的童鞋可以自己定义,自定义代码如下:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true"><shape android:shape="rectangle">        <stroke android:width="1.0px" android:color="#ffd3dde6" />        <corners android:bottomLeftRadius="5.0dip" android:bottomRightRadius="5.0dip" android:topLeftRadius="5.0dip" android:topRightRadius="5.0dip" />        <gradient android:angle="270.0" android:endColor="#ff0e9379" android:startColor="#ff0e9379" />    </shape></item>    <item android:state_focused="true">        <shape android:shape="rectangle">        <gradient android:angle="270.0" android:endColor="#ff0e9379" android:startColor="#ff0e9379" />        <stroke android:width="1.0px" android:color="#ffd3dde6" />        <corners android:bottomLeftRadius="5.0dip" android:bottomRightRadius="5.0dip" android:topLeftRadius="5.0dip" android:topRightRadius="5.0dip" />    </shape></item>    <item>        <shape android:shape="rectangle">            <gradient android:angle="270.0" android:endColor="#2B86E3" android:startColor="#2B86E3" />            <stroke android:width="1.0px" android:color="#ffd3dde6" />            <corners android:bottomLeftRadius="5.0dip" android:bottomRightRadius="5.0dip" android:topLeftRadius="5.0dip" android:topRightRadius="5.0dip" />        </shape>    </item></selector>

在说主方法之前,想说一点,就是我们经常看到在点击获得验证码时候按钮进行倒计时操作,当然,你可以自定义Button来写,也可以使用Handler来写,我使用的是TimeCount这个android提供的类进行的:

class  TimeCount extends CountDownTimer{        public TimeCount(long millisInFuture, long countDownInterval) {            super(millisInFuture, countDownInterval);        }        //计时过程显示        @Override        public void onTick(long millisUntilFinished) {            button.setTextSize(14);            button.setText(millisUntilFinished / 1000 + "秒后重新发送");            button.setClickable(false);            button.setBackgroundDrawable(getDrawable(R.drawable.bg_btn_shape_sending));        }        //计时完成触发        @Override        public void onFinish() {            button.setText("获取验证码");            button.setBackgroundDrawable(getDrawable(R.drawable.bg_btn_shape_login));            button.setClickable(true);        }    }

代码很简单,自己看也能看懂,这里就不多叙述了,不懂的童鞋自行百度。

Activity代码:

/** * Created by Chan on 2015/8/8 0008. */public class RegisterActivity extends Activity implements View.OnClickListener {    private Button button;    private EditText phoneNum,getNum;    private SMSCaptcha smsCaptcha;    private String phone;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.act_register);        //获得验证码实例        smsCaptcha=SMSCaptcha.getInstance();        button = (Button) findViewById(R.id.btn_register);        phoneNum = (EditText) findViewById(R.id.register_phone);        getNum= (EditText) findViewById(R.id.register_yanzhengma);        button.setOnClickListener(this);        findViewById(R.id.btn_register_sumbit).setOnClickListener(this);    }    //验证码按钮计时器    private TimeCount time;    @Override    public void onClick(View v) {        switch (v.getId()){            //简单判断手机号码格式            case R.id.btn_register:                    if (TextUtils.isEmpty(phoneNum.getText()))                    {                        Toast.makeText(RegisterActivity.this,"请输入手机号",Toast.LENGTH_SHORT).show();                        phoneNum.getText().clear();                    }                    else if (phoneNum.getText().toString().length()!=11)                    {                        Log.e("phoneNum.getText()",phoneNum.getText().toString().length()+"");                        phoneNum.getText().clear();                        Toast.makeText(RegisterActivity.this,"输入有误,请重新输入",Toast.LENGTH_SHORT).show();                    }                    else                    {                        //进行验证                        time=new TimeCount(60000,1000);//参数依次为总时长,计时时间间隔                        time.start();                        phone=phoneNum.getText().toString();                        //此方法用来向服务器发送请求验证码的请求,参数依次为你的电话,以及回调结果                        smsCaptcha.sendCaptcha(phone, new BaseData.ResultCallBack() {                            @Override                            public void onResult(int i, String reason, String result) {                                if (i!=0){                                    Toast.makeText(RegisterActivity.this,reason,Toast.LENGTH_SHORT).show();                                }                            }                        });                    }                break;            case R.id.btn_register_sumbit:                final ProgressDialog progressDialog=ProgressDialog.show(RegisterActivity.this,"请稍候","正在验证....");                String result=getNum.getText().toString().trim();                //提交验证码                smsCaptcha.commitCaptcha(phone, result, new BaseData.ResultCallBack() {                    @Override                    public void onResult(int code, String reason, String result)                    {                        if (code==0)                        {                            progressDialog.dismiss();                            Intent intent=new Intent(RegisterActivity.this,UserRegsiterActivtity.class);                            startActivity(intent);                        }                        else                        {                            progressDialog.dismiss();                            Toast.makeText(RegisterActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();                            return;                        }                    }                });                break;        }    }    class  TimeCount extends CountDownTimer{        public TimeCount(long millisInFuture, long countDownInterval) {            super(millisInFuture, countDownInterval);        }        //计时过程显示        @Override        public void onTick(long millisUntilFinished) {            button.setTextSize(14);            button.setText(millisUntilFinished / 1000 + "秒后重新发送");            button.setClickable(false);            button.setBackgroundDrawable(getDrawable(R.drawable.bg_btn_shape_sending));        }        //计时完成触发        @Override        public void onFinish() {            button.setText("获取验证码");            button.setBackgroundDrawable(getDrawable(R.drawable.bg_btn_shape_login));            button.setClickable(true);        }    }    @Override    protected void onDestroy() {        super.onDestroy();        if (time!=null)        {            time.cancel();            time=null;        }    }}

主要的方法就是两个

发送获取验证码请求

smsCaptcha.sendCaptcha(phone, new BaseData.ResultCallBack() {                            @Override                            public void onResult(int i, String reason, String result) {                                if (i!=0){                                    Toast.makeText(RegisterActivity.this,reason,Toast.LENGTH_SHORT).show();                                }                            }                        });

以及验证验证码正确与否请求

 smsCaptcha.commitCaptcha(phone, result, new BaseData.ResultCallBack() {                    @Override                    public void onResult(int code, String reason, String result)                    {                        if (code==0)                        {                            progressDialog.dismiss();                            Intent intent=new Intent(RegisterActivity.this,UserRegsiterActivtity.class);                            startActivity(intent);                        }                        else                        {                            progressDialog.dismiss();                            Toast.makeText(RegisterActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();                            return;                        }                    }                });

聚合数据有提供官方开发的api文档以及demo,不懂的童鞋可以自行查阅,写的很清楚。

0 0
原创粉丝点击