Android 短信驗證登錄(二):完成短信驗證功能

一、準(zhǔn)備工作:

我們退出服務(wù)中心,點擊下面的下載中心,


選擇SMSDK選項,下載資源,解壓資源,得到一個sms目錄,選擇sms\SMSSDK\SMSSDK\libs下的四個jar包,將四個jar包拷到APP項目的libs目錄下,如下圖所示:


在app(切換至project視野下)目錄下的build.gradle文件里寫入以下內(nèi)容:

implementation files('libs/MobCommons-2019.0508.1026.jar')

implementation files('libs/MobTools-2019.0507.1243.jar')

implementation files('libs/SDKWrapper-2018.0801.1824.jar')

implementation files('libs/SMSSDK-3.3.2.jar')


在main目錄下的AndroidManifest.xml中的manifest標(biāo)簽下添加下列:

<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.GET_TASKS" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

//訪問SD卡的應(yīng)用關(guān)聯(lián)目錄要聲明權(quán)限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

在寫的短信驗證登錄的Activity的標(biāo)簽下寫入(我的是SMSActivity):


二、代碼示例:

完成上面的步驟后基本的配置就結(jié)束了,下面就是寫邏輯代碼了,給出布局文件的代碼示例:

res目錄下的layout目錄下的activity_sms.xml:


<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_login" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.zjj.hc.doac.LoginActivity"> <ProgressBar android:id="@+id/login_progress" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:visibility="gone" /> <LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" android:layout_marginTop="0dp" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true"> <LinearLayout android:layout_height="50dp" android:layout_width="290dp" android:orientation="vertical" android:gravity="center" android:layout_gravity="center"> <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/textView" android:gravity="center" android:textStyle="bold" android:textSize="25dp" android:textColor="@android:color/black" android:text="短 信 登 錄"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp"> android:orientation="horizontal" android:layout_marginTop="20dp" <EditText android:id="@+id/mobile_login" android:layout_height="40dp" android:layout_width="350dp" android:textSize="17dp" android:paddingLeft="10dp" android:inputType="textPersonName" android:hint="請輸入手機(jī)號" android:maxLines="1" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp"> android:orientation="horizontal" android:layout_marginTop="20dp"> <EditText android:id="@+id/mobile_indentify" android:layout_height="40dp" android:layout_width="220dp" android:textSize="17dp" android:paddingLeft="10dp" android:inputType="textPersonName" android:hint="請輸入驗證碼" android:maxLines="1" android:singleLine="true" /> <Button android:id="@+id/get_indentify" android:layout_width="100dp" android:layout_height="wrap_content" android:text="獲取驗證碼"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp"> android:orientation="horizontal" android:layout_marginTop="20dp"> <Button android:id="@+id/logining_mobile" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登錄"/> </LinearLayout> </LinearLayout></RelativeLayout>

SMSActivity.java(額,代碼有些亂):

package com.zjj.hc.doac;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import cn.smssdk.EventHandler;import cn.smssdk.SMSSDK;import com.zjj.hc.R;import android.os.CountDownTimer;import java.util.regex.Matcher;import java.util.regex.Pattern;public class SMSActivity extends AppCompatActivity { private EditText mobileCodeText;//手機(jī)號輸入框 private EditText indentifyText;//驗證碼輸入框 private Button get_indentify;//獲取驗證碼 private Button login_btn;//登錄 private EventHandler eh; //創(chuàng)建事件接收器 private TimeCount secondCount ;//計時器 //創(chuàng)建線程設(shè)置驗證碼倒計時 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sms); initEvent(); init(); } private void init(){ eh = new EventHandler(){ @Override public void afterEvent(int event, int result, Object data) { if (result == SMSSDK.RESULT_COMPLETE) { //回調(diào)完成 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { //提交驗證碼成功 //跳轉(zhuǎn)成功 } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){ //獲取驗證碼成功 } else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){ //返回支持發(fā)送驗證碼的國家列表 } } else{ ((Throwable)data).printStackTrace(); } } }; //注冊短信回調(diào) SMSSDK.registerEventHandler(eh); } public void initEvent(){ mobileCodeText = (EditText) findViewById(R.id.mobile_login); indentifyText = (EditText) findViewById(R.id.mobile_indentify); get_indentify = (Button) findViewById(R.id.get_indentify); login_btn = (Button) findViewById(R.id.logining_mobile); get_indentify.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String mobileNums = mobileCodeText.getText().toString().trim(); //手機(jī)號是否為空 if(!mobileNums.equals("")){ //手機(jī)號不為空的情況下,驗證正確 if(isNomobile(mobileNums)){ //發(fā)送驗證碼 SMSSDK.getVerificationCode("+86",mobileCodeText.getText().toString()); secondCount.start(); }else{ Toast.makeText(SMSActivity.this, "手機(jī)號輸入錯誤", Toast.LENGTH_SHORT).show(); } }else{ Toast.makeText(SMSActivity.this, "手機(jī)號不能為空", Toast.LENGTH_SHORT).show(); } } }); login_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String mobileNums = mobileCodeText.getText().toString().trim(); String infintyNumbs = indentifyText.getText().toString().trim(); if(!mobileNums.equals("")){ if(isNomobile(mobileNums)){ if(!infintyNumbs.equals("")) { //提交驗證 SMSSDK.submitVerificationCode("+86", mobileCodeText.getText().toString().trim(), indentifyText.getText().toString().trim()); }else{ Toast.makeText(SMSActivity.this, "驗證碼不能為空", Toast.LENGTH_SHORT).show(); } }else{ Toast.makeText(SMSActivity.this, "手機(jī)號輸入錯誤", Toast.LENGTH_SHORT).show(); } }else{ Toast.makeText(SMSActivity.this, "手機(jī)號不能為空", Toast.LENGTH_SHORT).show(); } } }); secondCount = new TimeCount(60000,1000); } //判斷輸入的手機(jī)號是否符合規(guī)范 public boolean isNomobile(String mobileCode){ //正則表達(dá)式判斷 Pattern pattern = Pattern.compile("^(13[0-9]|15([0-3]|[5-9])|14[5,7,9]|17[1,3,5,6,7,8]|18[0-9])\\d{8}$"); Matcher matcher = pattern.matcher(mobileCode); if(matcher.matches()){ return true; }else return false; } protected void onDestroy() { super.onDestroy(); SMSSDK.unregisterEventHandler(eh); } class TimeCount extends CountDownTimer { public TimeCount(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onTick(long l) { get_indentify.setClickable(false); get_indentify.setText(l/1000 + "秒后重試"); } @Override public void onFinish() { get_indentify.setClickable(true); get_indentify.setText("獲取驗證碼"); } }}


三、運(yùn)行效果:


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容