Android如何实现获取短信验证码的功能

  • Android如何实现获取短信验证码的功能
  • 连雨独饮 发表于 2015/7/5 12:59:00 | 分类标签: Android 短信 验证码
  • 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:



    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    <? 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、设计我们的布局文件:

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    < 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对象,用于当服务器发送验证码后,提醒用户注意。

    最后附图两张,供大家参考:
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值