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

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

  官方为我们提供了两种设计方式:第一种调用内部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:在http://dashboard.mob.com/注册一个账号后,创建一个发送短信的应用,系统会自动为生成appkey和appsecret

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

  最后附图两张,供大家参考:

  

  

Android获取短信验证码的更多相关文章

  1. android 获取短信验证码倒计时

    android 获取短信验证码倒计时 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWVuZ2xlbGUxMzE0/font/5a6L5L2T/fonts ...

  2. android发送短信验证码并自动获取验证码填充文本框

    android注册发送短信验证码并自动获取短信,截取数字验证码填充文本框. 一.接入短信平台 首先需要选择短信平台接入,这里使用的是榛子云短信平台(http://smsow.zhenzikj.com) ...

  3. 『实践』Android之短信验证码(用的Mob短信验证)

    1.参考资料 Mob网站:http://www.mob.com/ Mob在Github上的例子:https://github.com/MobClub/SMSSDK-for-Android 教程:htt ...

  4. jQuery获取短信验证码+倒计时实现

    jQuery 短信验证码倒计时 <script type="text/javascript" charset="utf-8"> $(function ...

  5. PHP获取短信验证码

    PHP如何获取短信验证码?以下是创蓝253短信平台下的PHP接口代码案例:   <?php header("Content-type:text/html; charset=UTF-8& ...

  6. iOS点击获取短信验证码按钮

    概述 iOS点击获取短信验证码按钮, 由于 Demo整体测试运行效果 , 整个修改密码界面都已展现, 并附送正则表达式及修改密码逻辑. 详细 代码下载:http://www.demodashi.com ...

  7. python+pymssql+selenium 获取短信验证码登录(实战练习)

    登录页面输入手机号, 获取短信验证码(验证码有10分钟有效期) 1 连接sql server数据库,获取10分钟之内的有效短信验证码 2 页面输入手机号,并获取验证码.若存在有效验证码则输入验证码,若 ...

  8. 23、vue实现获取短信验证码

    1.html页面: <el-form-item prop="phoneCode" class="pr"> <el-input placehol ...

  9. 转载:Android自动化测试- 自动获取短信验证码

    前言:android应用的自动化测试必然会涉及到注册登录功能,而许多的注册登录或修改密码功能常常需要输入短信验证码,因此有必要能够自动获得下发的短信验证码. 主要就是实时获取短信信息. android ...

随机推荐

  1. [机器学习] 深度学习之caffe1——软件配置与测试

    caffe的编译配置真的是很让人头疼啊,不知道试过多少次了~~~ 重装系统了七八次,搞得linux的一些常用命令倒是很熟悉了~~~ 我有洁癖~~~某一个点上出了错,我一定要把它搞好了,再重新来一次,我 ...

  2. Visual Studio 2013执行项目报错:HTTP 错误 500.22

    转至:http://www.codingwhy.com/410.html 具体报错 HTTP 错误 500.22 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ...

  3. 使用nose 进行Python项目的自动化测试

    一.为什么使用nose? 编写测试更容易.nose可以自动识别继承于unittest.TestCase的测试单元,并执行测试,而且,nose也可以测试非继承于unittest.TestCase的测试单 ...

  4. js中使用new Date(str)创建时间对象不兼容firefox和ie的解决方式

    /** * 解决 ie,火狐浏览器不兼容new Date(s) * @param strDate * 返回 date对象 * add by zyf at 2015年11月5日 */ function ...

  5. mas_makeConstraints && mas_remakeConstraints && mas_updateConstraints 用法与注意事项

    mas_makeConstraints && mas_remakeConstraints && mas_updateConstraints 用法与注意事项 字数400 ...

  6. duplicate symbols for architecture arm64 after xCode 8.0 update

    Xcode IDE  从7.3.1 update 到 8.0 之后出现的问题 一个错误把我困扰了两天之久,最终找到解决办法我欣喜若狂. 错误发生原因:Xcode IDE  从7.3.1 update ...

  7. MyBatis的经典案例

    1.首先我们先了解Mybatis的一些jar包 ---和项目框架 2.接下来就看看mybatis的配置文件(mybatis-config.xml) <?xml version="1.0 ...

  8. js将数字转成大写中文

    <script type="text/javascript"> //主函数 function DX(n) { if (!/^(0|[1-9]\d*)(\.\d+)?$/ ...

  9. nginx超时重发

    最近一直遇到一个bug: 客户端会二次请求服务端,服务端多次调用remote服务. 特点是,这些请求都是模型切片相关的,耗时很长的请求,往往需要1分钟左右. 开始以为是客户端代码有问题,进行了二次请求 ...

  10. 封装自己的DB类(PHP)

    封装一个DB类,用来专门操作数据库,以后凡是对数据库的操作,都由DB类的对象来实现.这样有了自己的DB类,写项目时简单的sql语句就不用每次写了,直接调用就行,很方便! 1.封装一个DB类.一个类文件 ...