| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
LayNcl
7年前发布

Android Studio Mob快速集成短信验证(图文教程)

   <h3>前言:</h3>    <p>现在APP应用都是需要通过手机获取短信验证码来注册应用或是实现其它功能 。而利用短信验证码来注册会员,大大降低了非法注册,很大程度上提高了用户账户的安全性,同时避免一些黑名单用户的骚扰。目前市面上已经有了很多提供短信验证的服务商,有收费的,也有免费的。而我们作为个人开发者或者公司为了节约成本,就需要用到下面介绍的一个免费的短信验证平台--- <a href="/misc/goto?guid=4959732960737503918" rel="nofollow,noindex">Mob.com</a></p>    <p>Mob平台的优点,这里我们就不再多说,直接开始看教程如何使用。</p>    <p><strong>1.注册开发者</strong>( <a href="/misc/goto?guid=4959732960840529806" rel="nofollow,noindex">http://reg.mob.com/)</a></p>    <p>只有注册了用户,才可以创建应用,使用第Mob平台提供的短信验证工具。(已有账号这步可以省略)</p>    <p><strong>2.登录官网首页</strong></p>    <p>点击右侧头像栏那里,选择进入后台</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/c37d65f96e65d717f80d2e7b6034ce11.png"></p>    <p><strong>3.选择SecurityCodeSDK</strong></p>    <p>立即使用并创建名为MobSmsDemo的Android应用</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/8e419bda3f49023141944a0d20fab9a7.png"></p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/9e7aa332bd23e3c394127c1c31c436ab.png"></p>    <p><strong>4.进入SecurityCodeSDK后台</strong></p>    <p>获取创建应用成功后的APP Key和 APP Secret</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/cd66dfed5d470bb9c38772fc9e330e15.png"></p>    <p><strong>5.回到首页下载 SMS For Android短信验证码SDK</strong></p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/fd475fbd6a42086514e7e9dc01f91a85.png"></p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/ac9a5082828d827ae7d960d4d062a49a.png"></p>    <p><strong>6.下载完成后得到的是一个压缩包,解压缩,打开SMSSDK这个文件夹</strong></p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/f5931bf75b949b37aaf26d366c8dcec8.png"></p>    <p><strong>7.HowToUse.txt文档是Mob平台提供给我们需要注意的两个地方点,这里简要看过一遍</strong></p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/558bf61b02a0c4f3cc7ea665cc80d5da.png"></p>    <p><strong>8.在这里我们不需要用GUI库,所以把除了SMSSDKGUI-2.1.2.aar其他三个文件拷贝到项目中,放在Module所在的Libs里面,在build.gradle中进行配置添加依赖</strong></p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/07b7c47e7a7bdc95412ff9bf78e56b27.png"></p>    <p><strong>9.在AndroidManifest.xml配置相应的权限,然后在“application”下添加如下activity</strong></p>    <pre>  <code class="language-java"><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.READ_SMS" />  <uses-permission android:name="android.permission.GET_TASKS" />  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /></code></pre>    <pre>  <code class="language-java"><activity  android:name="com.mob.tools.MobUIShell"  android:theme="@android:style/Theme.Translucent.NoTitleBar"  android:configChanges="keyboardHidden|orientation|screenSize"  android:windowSoftInputMode="stateHidden|adjustResize"/></code></pre>    <p><strong>10.使用上面获取到的APP Key和 APP Secret,初始化SDK</strong></p>    <pre>  <code class="language-java">//initSDK方法是短信SDK的入口,需要传递您从MOB应用管理后台中注册的SMSSDK的应用AppKey和AppSecrete,如果填写错误,后续的操作都将不能进行  SMSSDK.initSDK(MainActivity.this, "196e511258800", "ba98d7e9aa85eaa323cb4dc60435fd16");</code></pre>    <p><strong>11.registerEventHandler是用来往SMSSDK中注册一个事件接收器。</strong></p>    <pre>  <code class="language-java">EventHandler eventHandler = 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(eventHandler);</code></pre>    <p><strong>12.由于EventHandler开启了线程,不能直接在afterEvent中更新UI,所以还需要在MainActivity当中定义一个Handler来接受EventHandler发送过来的消息。</strong></p>    <pre>  <code class="language-java">Handler handler = new Handler() {  public void handleMessage(Message msg) {      if (msg.what == -1) {          //修改控件文本进行倒计时  i 以60秒倒计时为例          btnSendMsg.setText( i+" s");      } else if (msg.what == -2) {          //修改控件文本,进行重新发送验证码          btnSendMsg.setText("重新发送");          btnSendMsg.setClickable(true);          i = 60;      } else {          int event = msg.arg1;          int result = msg.arg2;          Object data = msg.obj;            // 短信注册成功后,返回MainActivity,然后提示          if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {          // 提交验证码成功,调用注册接口,之后直接登录          //当号码来自短信注册页面时调用登录注册接口          //当号码来自绑定页面时调用绑定手机号码接口              } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {          Toast.makeText(getApplicationContext(), "验证码已经发送",                  Toast.LENGTH_SHORT).show();              } else {                  ((Throwable) data).printStackTrace();              }          } else if (result == SMSSDK.RESULT_ERROR) {              try {                  Throwable throwable = (Throwable) data;                  throwable.printStackTrace();                  JSONObject object = new JSONObject(throwable.getMessage());                  String des = object.optString("detail");//错误描述                  int status = object.optInt("status");//错误代码                  if (status > 0 && !TextUtils.isEmpty(des)) {                      Toast.makeText(MainActivity.this, des, Toast.LENGTH_SHORT).show();                      return;                  }              } catch (Exception e) {                  //do something              }          }      }     }  };</code></pre>    <p><strong>13.在布局上定义两个按钮控件进行发送短信和验证短信验证码并监听点击事件。</strong></p>    <pre>  <code class="language-java">@Override  public void onClick(View v) {      String phoneNum=etPhoneNumber.getText().toString().trim();      switch (v.getId()) {          case R.id.btnSendMsg:              if (TextUtils.isEmpty(phoneNum)) {                  Toast.makeText(getApplicationContext(),"手机号码不能为空",                          Toast.LENGTH_SHORT).show();                  return;              }              SMSSDK.getVerificationCode("86", phoneNum);              btnSendMsg.setClickable(false);              //开始倒计时              new Thread(new Runnable() {                  @Override                  public void run() {                      for (; i > 0; i--) {                          handler.sendEmptyMessage(-1);                          if (i <= 0) {                              break;                          }                          try {                              Thread.sleep(1000);                          } catch (InterruptedException e) {                              e.printStackTrace();                          }                      }                      //倒计时结束执行                      handler.sendEmptyMessage(-2);                  }              }).start();              break;          case R.id.btnSubmitCode:               String code = etCode.getText().toString().trim();              if (TextUtils.isEmpty(phoneNum)) {                  Toast.makeText(getApplicationContext(),"手机号码不能为空",                          Toast.LENGTH_SHORT).show();                  return;              }              if (TextUtils.isEmpty(code)) {                  Toast.makeText(getApplicationContext(),"验证码不能为空",                      Toast.LENGTH_SHORT).show();                      return;              }              SMSSDK.submitVerificationCode("86", phoneNum, code);              break;          default:              break;      }  }</code></pre>    <p><strong>14.在完成短信验证之后,需要销毁回调监听接口。</strong></p>    <pre>  <code class="language-java">@Override  protected void onDestroy() {      super.onDestroy();      SMSSDK.unregisterAllEventHandler();  }</code></pre>    <p><strong>15.最后看下布局UI和效果图。</strong></p>    <pre>  <code class="language-java"><?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:background="@android:color/white"      android:orientation="vertical">      <LinearLayout          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:layout_marginLeft="30dp"          android:layout_marginRight="30dp"          android:layout_marginTop="60dp"          android:orientation="horizontal">              <TextView              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="手机号:"              android:textColor="#222222"              android:textSize="16sp" />            <EditText              android:id="@+id/etPhoneNumber"              android:layout_width="match_parent"              android:layout_height="wrap_content"              android:layout_marginRight="10dp"              android:background="@null"              android:drawablePadding="10dp"              android:hint="请输入手机号"              android:inputType="phone"              android:lines="0"              android:maxLength="11"              android:paddingBottom="10dp"              android:paddingLeft="20dp"              android:paddingTop="10dp"              android:text="13691942911"              android:textColor="@android:color/black"              android:textColorHint="#cccccc"              android:textSize="16sp" />        </LinearLayout>        <View          android:layout_width="match_parent"          android:layout_height="1dp"          android:layout_marginLeft="30dp"          android:layout_marginRight="30dp"          android:background="@android:color/darker_gray" />        <LinearLayout          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:layout_marginLeft="30dp"          android:layout_marginRight="30dp"          android:layout_marginTop="10dp"          android:orientation="horizontal">            <TextView              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="验证码:"              android:textColor="#222222"              android:textSize="16sp" />            <EditText              android:id="@+id/etCode"              android:layout_width="0dp"              android:layout_height="wrap_content"              android:layout_weight="1"              android:background="@null"              android:hint="请输入验证码"              android:inputType="phone"              android:lines="0"              android:maxLength="6"              android:paddingBottom="10dp"              android:paddingLeft="20dp"              android:paddingTop="10dp"              android:textColor="@android:color/black"              android:textColorHint="#cccccc"              android:textSize="16sp" />            <Button              android:id="@+id/btnSendMsg"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_marginRight="20dp"              android:background="@android:color/darker_gray"              android:text="获取验证码"              android:textColor="#0000ff"              android:textSize="15sp" />      </LinearLayout>        <View          android:layout_width="match_parent"          android:layout_height="1dp"          android:layout_marginLeft="30dp"          android:layout_marginRight="30dp"          android:background="@android:color/darker_gray" />        <Button          android:id="@+id/btnSubmitCode"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:background="@android:color/darker_gray"          android:gravity="center"          android:layout_marginLeft="30dp"          android:layout_marginRight="30dp"          android:layout_marginTop="30dp"          android:text="验证"          android:textColor="#0000ff"          android:textSize="15sp" />  </LinearLayout></code></pre>    <p>最后看下效果图:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/8ee583769f014dc275711e140d430c10.jpg"></p>    <p> </p>    <p> </p>    <p>来自:http://www.jianshu.com/p/7832e3f5a213</p>    <p> </p>    
 本文由用户 LayNcl 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1483415636375.html
Android开发 移动开发 Android Studio