当前位置: 移动技术网 > IT编程>移动开发>Android > Android获取短信验证码的实现方法

Android获取短信验证码的实现方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

广东外语艺术职业学院分数线,徐博杰,徒狼刀铺

先给大家展示下效果图,如果感觉不错,请参考实现思路详解

android开发中关于短息验证码的设计层出不穷,越来越多的应用为了更好的提高软件的安全性,开始使用通过服务器向用户发送验证码的方式,来保护用户个人信息的安全性。无论是用户注册时的信息验证还是当用户发出找回密码请求时的短信验证,他们的工作原理大致上是一致的,因为项目的需要研究了一下关于这方面的知识,本篇我将带领大家一起实现这一当下流行的设计方案。

众所周知,短信验证需要服务器端生成一个验证码,然后发送到用户输入的手机上,这个过程需要服务器主动向客户发送验证短信,所以这是就需要一个移动或联通的发送短息接口,由于本人目前尚处于学生阶段,没有获得这个接口的权限,所以我就选择了借助网上的移动开发服务平台,来完成这个功能的实现,这里我借用的平台是:,大家可以关注一下,这个平台为我们开发移动应用提供了很好的技术指导,可以大大缩短我们的开发周期。废话不多说,下面开始我们今天的重点。

官方为我们提供了两种设计方式:第一种调用内部gui实现;另一种通过自定义gui实现,对于第一种方式,我就不再多讲,因为官方文档提供了很详细的实行步骤,大家只需要按照上面的步骤去实现即可,没有难度。本篇我将带领大家通过自定义gui实现短信验证功能。首先开发之前你可以先查阅一下官方提供的无gui api,然后下载一下官方提供的dome,做好这些工作之后,我们就可以开始我们的设计了。

1、将demo中的libs下的smssdk-1.1.5.jar和armeabi文件夹拷贝到我们项目的libs目录下,这是官方提供的类库jar包。

2、在androidmanifest.xml文件添加权限和声明action:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_sms"
android:versioncode="1"
android:versionname="1.0" >
<uses-sdk
android:minsdkversion="8"
android:targetsdkversion="18" />
<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.receive_sms" />
<uses-permission android:name="android.permission.get_tasks" />
<uses-permission android:name="android.permission.access_fine_location" />
<application
android:allowbackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/apptheme" >
<activity
android:name="com.example.android_sms.mainactivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.main" />
<category android:name="android.intent.category.launcher" />
</intent-filter>
</activity>
<activity
android:name="cn.smssdk.smssdkuishell"
android:configchanges="keyboardhidden|orientation|screensize"
android:theme="@android:style/theme.translucent.notitlebar"
android:windowsoftinputmode="statehidden|adjustresize" />
</application>
</manifest>

3、设计我们的布局文件:

<relativelayout 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"
tools:context=".mainactivity" >
<textview
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerhorizontal="true"
android:layout_margintop="20dp"
android:text="短信验证"
android:textcolor="#00ffaa"
android:textsize="20dp" />
<textview
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignparentleft="true"
android:layout_below="@+id/textview2"
android:layout_marginleft="20dp"
android:layout_margintop="20dp"
android:text="手机号:" />
<edittext
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignbaseline="@+id/textview1"
android:layout_alignbottom="@+id/textview1"
android:layout_torightof="@+id/textview1"
android:maxlength="11"
android:ems="11"
android:inputtype="phone" >
<requestfocus />
</edittext>
<textview
android:id="@+id/textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignleft="@+id/textview1"
android:layout_margintop="40dp"
android:layout_below="@+id/phone"
android:text="验证码:"/>
<edittext
android:id="@+id/cord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margintop="20dp"
android:layout_alignbaseline="@+id/textview3"
android:layout_alignbottom="@+id/textview3"
android:layout_alignleft="@+id/phone"
android:ems="4"
android:maxlength="4"
android:inputtype="phone" />
<button
android:id="@+id/getcord"
style="?android:attr/buttonstylesmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_aligntop="@+id/cord"
android:layout_marginleft="20dp"
android:layout_margintop="10dp"
android:layout_torightof="@+id/cord"
android:visibility="visible"
android:text="获取验证码" />
<button
android:id="@+id/savecord"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/cord"
android:layout_margin="20dp"
android:text="验证" />
<textview
android:id="@+id/now"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/savecord"
android:layout_torightof="@+id/cord"
android:gravity="center_horizontal"
android:visibility="gone"
android:text="提示信息"
android:textcolor="#aaaaaa" />
</relativelayout>

4、我们的mainactivity:

/**
* 自定义gui短信验证
* @time: 2015年7月4日
*/
public class mainactivity extends activity implements onclicklistener{
private edittext phone;
private edittext cord;
private textview now;
private button getcord;
private button savecord;
private string iphone;
private string icord;
private int time = 60;
private boolean flag = true;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
requestwindowfeature(window.feature_no_title);
setcontentview(r.layout.activity_main);
init();
     smssdk.initsdk(this, "<您的appkey>", "<您的appsecret>");
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);
}
private void init() {
phone = (edittext) findviewbyid(r.id.phone);
cord = (edittext) findviewbyid(r.id.cord);
now = (textview) findviewbyid(r.id.now);
getcord = (button) findviewbyid(r.id.getcord);
savecord = (button) findviewbyid(r.id.savecord);
getcord.setonclicklistener(this);
savecord.setonclicklistener(this);
}
@override
public void onclick(view v) {
switch (v.getid()) {
case r.id.getcord:
if(!textutils.isempty(phone.gettext().tostring().trim())){
if(phone.gettext().tostring().trim().length()==11){
iphone = phone.gettext().tostring().trim();
smssdk.getverificationcode("86",iphone);
cord.requestfocus();
getcord.setvisibility(view.gone);
}else{
toast.maketext(mainactivity.this, "请输入完整电话号码", toast.length_long).show();
phone.requestfocus();
}
}else{
toast.maketext(mainactivity.this, "请输入您的电话号码", toast.length_long).show();
phone.requestfocus();
}
break;
case r.id.savecord:
if(!textutils.isempty(cord.gettext().tostring().trim())){
if(cord.gettext().tostring().trim().length()==4){
icord = cord.gettext().tostring().trim();
smssdk.submitverificationcode("86", iphone, icord);
flag = false;
}else{
toast.maketext(mainactivity.this, "请输入完整验证码", toast.length_long).show();
cord.requestfocus();
}
}else{
toast.maketext(mainactivity.this, "请输入验证码", toast.length_long).show();
cord.requestfocus();
}
break;
default:
break;
}
}
//验证码送成功后提示文字
private void remindertext() {
now.setvisibility(view.visible);
handlertext.sendemptymessagedelayed(1, 1000);
}
handler handlertext =new handler(){
public void handlemessage(message msg) {
if(msg.what==1){
if(time>0){
now.settext("验证码已发送"+time+"秒");
time--;
handlertext.sendemptymessagedelayed(1, 1000);
}else{
now.settext("提示信息");
time = 60;
now.setvisibility(view.gone);
getcord.setvisibility(view.visible);
}
}else{
cord.settext("");
now.settext("提示信息");
time = 60;
now.setvisibility(view.gone);
getcord.setvisibility(view.visible);
}
};
};
handler handler=new handler(){
@override
public void handlemessage(message msg) {
// todo auto-generated method stub
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) {
//短信注册成功后,返回mainactivity,然后提示新好友
if (event == smssdk.event_submit_verification_code) {//提交验证码成功,验证通过
toast.maketext(getapplicationcontext(), "验证码校验成功", toast.length_short).show();
handlertext.sendemptymessage(2);
} else if (event == smssdk.event_get_verification_code){//服务器验证码发送成功
remindertext();
toast.maketext(getapplicationcontext(), "验证码已经发送", toast.length_short).show();
}else if (event ==smssdk.event_get_supported_countries){//返回支持发送验证码的国家列表
toast.maketext(getapplicationcontext(), "获取国家列表成功", toast.length_short).show();
}
} else {
if(flag){
getcord.setvisibility(view.visible);
toast.maketext(mainactivity.this, "验证码获取失败,请重新获取", toast.length_short).show();
phone.requestfocus();
}else{
((throwable) data).printstacktrace();
int resid = getstringres(mainactivity.this, "smssdk_network_error");
toast.maketext(mainactivity.this, "验证码错误", toast.length_short).show();
cord.selectall();
if (resid > 0) {
toast.maketext(mainactivity.this, resid, toast.length_short).show();
}
}
}
}
};
@override
protected void ondestroy() {
super.ondestroy();
smssdk.unregisteralleventhandler();
}
}

注:appkey和appsecret:在注册一个账号后,创建一个发送短信的应用,系统会自动为生成appkey和appsecret

handlertext是我自定义设计的handker对象,用于当服务器发送验证码后,提醒用户注意。

以上所述是小编给大家介绍的android获取短信验证码的实现方法,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网