配置弄好了就开始写代码了,因为现在是完成功能,所以画面做的比较难看。目前SMSSDK提供两套接口方案,一种是默认的UI集成即可。另一种是不使用他们的UI,直接调用发送和验证接口。所以我用的是第二种。
思路就是:首先写手机号,获取验证码,输入验证码,然后提示登录成功。下图是我的界面设计,每个组件的ID通过名字就知道是什么意思。
<?xml version="1.0" enCoding="utf-8"?><androIDx.constraintlayout.Widget.ConstraintLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" xmlns:app="http://schemas.androID.com/apk/res-auto" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context=".MainActivity"> <EditText androID:ID="@+ID/editTextPhoneNum" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ems="10" androID:hint="手机号" androID:inputType="textPersonname" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.161" app:layout_constraintStart_toStartOf="parent" app:layout_constrainttop_totopOf="parent" app:layout_constraintVertical_bias="0.11" /> <button androID:ID="@+ID/buttonCode" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:hint="获取验证码" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.914" app:layout_constraintStart_toStartOf="parent" app:layout_constrainttop_totopOf="parent" app:layout_constraintVertical_bias="0.105" tools:text="获取验证码" /> <EditText androID:ID="@+ID/editTextCode" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ems="10" androID:hint="验证码" androID:importantForautofill="no" androID:inputType="textPersonname" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.161" app:layout_constraintStart_toStartOf="parent" app:layout_constrainttop_totopOf="parent" app:layout_constraintVertical_bias="0.215" /> <button androID:ID="@+ID/buttonLogin" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:hint="登录" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.897" app:layout_constraintStart_toStartOf="parent" app:layout_constrainttop_totopOf="parent" app:layout_constraintVertical_bias="0.21" /></androIDx.constraintlayout.Widget.ConstraintLayout>
MainActivity.java首先是获取验证码,如果你没有输入手机号或者手机号不符合规则,会有提示信息。验证码获取不了。其次是如果手机号符合了第一条内容,则调用java SMSSDK.getVerificationCode(country, phone);
来获得手机验证码(请求验证码,其中country表示国家代码,如“86”;phone表示手机号码,如“13800138000”)。当收到验证码时候输入验证码进行校验,调用java SMSSDK.submitVerificationCode(country, phone, code);
来进行校验,对则登录成功,错则登录失败。package com.example.phonecode;import androID.os.Bundle;import androID.os.CountDownTimer;import androID.text.TextUtils;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Toast;import androIDx.appcompat.app.AppCompatActivity;import org.Json.JsONException;import org.Json.JsONObject;import cn.smssdk.EventHandler;import cn.smssdk.SMSSDK;public class MainActivity extends AppCompatActivity { private button buttonCode,buttonLogin; private EditText editTextPhoneNum,editTextCode; private String phoneNum,code; private EventHandler eh; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); buttonCode = findVIEwByID(R.ID.buttonCode); buttonLogin = findVIEwByID(R.ID.buttonLogin); editTextCode = findVIEwByID(R.ID.editTextCode); editTextPhoneNum = findVIEwByID(R.ID.editTextPhoneNum); 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) { //提交验证码成功 runOnUiThread(new Runnable() { @OverrIDe public voID run() { Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show(); } }); }else if (event == SMSSDK.EVENT_GET_VOICE_VERIFICATION_CODE){ runOnUiThread(new Runnable() { @OverrIDe public voID run() { Toast.makeText(MainActivity.this,"语音验证发送",Toast.LENGTH_SHORT).show(); } }); } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){ //获取验证码成功 runOnUiThread(new Runnable() { @OverrIDe public voID run() { Toast.makeText(MainActivity.this,"验证码已发送",Toast.LENGTH_SHORT).show(); } }); }else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){ Log.i("test","test"); } }else{ ((Throwable)data).printstacktrace(); Throwable throwable = (Throwable) data; throwable.printstacktrace(); Log.i("1234",throwable.toString()); try { JsONObject obj = new JsONObject(throwable.getMessage()); final String des = obj.optString("detail"); if (!TextUtils.isEmpty(des)){ runOnUiThread(new Runnable() { @OverrIDe public voID run() { Toast.makeText(MainActivity.this,des,Toast.LENGTH_SHORT).show(); } }); } } catch (JsONException e) { e.printstacktrace(); } } } }; //注册一个事件回调监听,用于处理SMSSDK接口请求的结果 SMSSDK.registerEventHandler(eh); buttonCode.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { phoneNum = editTextPhoneNum.getText().toString(); if(!phoneNum.isEmpty()){ if(Utils.checkTel(phoneNum)){ //利用正则表达式获取检验手机号 // 获取验证码 SMSSDK.getVerificationCode("86", phoneNum); }else{ Toast.makeText(getApplicationContext(),"请输入有效的手机号",Toast.LENGTH_LONG).show(); } }else { Toast.makeText(getApplicationContext(),"请输入手机号",Toast.LENGTH_LONG).show(); return; } phoneNum = editTextPhoneNum.getText().toString(); } }); buttonLogin.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { code = editTextCode.getText().toString(); if(!code.isEmpty()){ //提交验证码 SMSSDK.submitVerificationCode("86", phoneNum, code); }else{ Toast.makeText(getApplicationContext(),"请输入验证码",Toast.LENGTH_LONG).show(); return; } } }); } // 使用完EventHandler需注销,否则可能出现内存泄漏 @OverrIDe protected voID onDestroy() { super.onDestroy(); SMSSDK.unregisterEventHandler(eh); }}
手机号校验package com.example.phonecode;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Utils { /** * 正则匹配手机号码: */ public static boolean checkTel(String tel){ Pattern p = Pattern.compile("^[1][3,4,5,7,8,9][0-9]{9}$"); Matcher matcher = p.matcher(tel); return matcher.matches(); }}
选权限说明:将下面的代码添加到AndroIDManifest.xml里,如下图<!-- 可选权限说明--><uses-permission androID:name="androID.permission.INTERNET" />允许应用程序联网(必须) <uses-permission androID:name="androID.permission.ACCESS_WIFI_STATE" />允许访问WiFi网络状态信息(必须) <uses-permission androID:name="androID.permission.READ_PHONE_STATE" />允许读取手机状态(必须) <uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE" />允许访问网络状态(必须) <uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" />允许写手机存储(必须) <uses-permission androID:name="androID.permission.RECEIVE_SMS" />GUI自动填充验证码功能(非必须) <uses-permission androID:name="androID.permission.READ_SMS" />从短信中读取验证码接口(非必须) <uses-permission androID:name="androID.permission.READ_CONTACTS" />应用内好友功能(非必须)
运行结果:手机号为空,提示输入手机号手机号格式不正确提示
手机号正确输入
验证码输入错误验证码输入正确后面两个功能就不实现了,截图时候忘了给手机打马赛克了,读者自己实现吧。有什么不懂得,可以留言,指出。如果有大佬发现问题或者提供更好得方法,请留言,小弟感激不尽。 总结
以上是内存溢出为你收集整理的android------实现手机验证码登录全部内容,希望文章能够帮你解决android------实现手机验证码登录所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)