引言:今天研究了一下我們做的項目的功能。做了關於手機驗證碼的功能。做的還是比較丑,目前只是實現功能,美化會再后期進行。
正文:我用的時MobTech。前兩步操作我就不說了,自己看文檔解決,很簡單。我主要說說文里沒有的(我沒找到的)。
配置弄好了就開始寫代碼了,因為現在是完成功能,所以畫面做的比較難看。目前SMSSDK提供兩套接口方案,一種是默認的UI集成即可。另一種是不使用他們的UI,直接調用發送和驗證接口。所以我用的是第二種。
思路就是:首先寫手機號,獲取驗證碼,輸入驗證碼,然后提示登錄成功。下圖是我的界面設計,每個組件的id通過名字就知道是什么意思。
- 頁面布局.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextPhoneNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="手機號"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.161"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.11" />
<Button
android:id="@+id/buttonCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="獲取驗證碼"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.914"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.105"
tools:text="獲取驗證碼" />
<EditText
android:id="@+id/editTextCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="驗證碼"
android:importantForAutofill="no"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.161"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.215" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="登錄"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.897"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.21" />
</androidx.constraintlayout.widget.ConstraintLayout>
- MainActivity.java
- 首先是獲取驗證碼,如果你沒有輸入手機號或者手機號不符合規則,會有提示信息。驗證碼獲取不了。
- 其次是如果手機號符合了第一條內容,則調用
java SMSSDK.getVerificationCode(country, phone);
來獲得手機驗證碼(請求驗證碼,其中country表示國家代碼,如“86”;phone表示手機號碼,如“13800138000”)。 - 當收到驗證碼時候輸入驗證碼進行校驗,調用
java SMSSDK.submitVerificationCode(country, phone, code);
來進行校驗,對則登錄成功,錯則登錄失敗。
package com.example.phonecode;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import org.json.JSONException;
import org.json.JSONObject;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
public class MainActivity extends AppCompatActivity {
private Button buttonCode,buttonLogin;
private EditText editTextPhoneNum,editTextCode;
private String phoneNum,code;
private EventHandler eh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonCode = findViewById(R.id.buttonCode);
buttonLogin = findViewById(R.id.buttonLogin);
editTextCode = findViewById(R.id.editTextCode);
editTextPhoneNum = findViewById(R.id.editTextPhoneNum);
eh = new EventHandler() {
@Override
public void afterEvent(int event, int result, Object data) {
if (result == SMSSDK.RESULT_COMPLETE){
//回調完成
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
//提交驗證碼成功
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"登錄成功",Toast.LENGTH_SHORT).show();
}
});
}else if (event == SMSSDK.EVENT_GET_VOICE_VERIFICATION_CODE){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"語音驗證發送",Toast.LENGTH_SHORT).show();
}
});
}
else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
//獲取驗證碼成功
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"驗證碼已發送",Toast.LENGTH_SHORT).show();
}
});
}else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
Log.i("test","test");
}
}else{
((Throwable)data).printStackTrace();
Throwable throwable = (Throwable) data;
throwable.printStackTrace();
Log.i("1234",throwable.toString());
try {
JSONObject obj = new JSONObject(throwable.getMessage());
final String des = obj.optString("detail");
if (!TextUtils.isEmpty(des)){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,des,Toast.LENGTH_SHORT).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
//注冊一個事件回調監聽,用於處理SMSSDK接口請求的結果
SMSSDK.registerEventHandler(eh);
buttonCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
phoneNum = editTextPhoneNum.getText().toString();
if(!phoneNum.isEmpty()){
if(Utils.checkTel(phoneNum)){ //利用正則表達式獲取檢驗手機號
// 獲取驗證碼
SMSSDK.getVerificationCode("86", phoneNum);
}else{
Toast.makeText(getApplicationContext(),"請輸入有效的手機號",Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(getApplicationContext(),"請輸入手機號",Toast.LENGTH_LONG).show();
return;
}
phoneNum = editTextPhoneNum.getText().toString();
}
});
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
code = editTextCode.getText().toString();
if(!code.isEmpty()){
//提交驗證碼
SMSSDK.submitVerificationCode("86", phoneNum, code);
}else{
Toast.makeText(getApplicationContext(),"請輸入驗證碼",Toast.LENGTH_LONG).show();
return;
}
}
});
}
// 使用完EventHandler需注銷,否則可能出現內存泄漏
@Override
protected void onDestroy() {
super.onDestroy();
SMSSDK.unregisterEventHandler(eh);
}
}
- 手機號校驗
package com.example.phonecode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Utils {
/**
* 正則匹配手機號碼:
*/
public static boolean checkTel(String tel){
Pattern p = Pattern.compile("^[1][3,4,5,7,8,9][0-9]{9}$");
Matcher matcher = p.matcher(tel);
return matcher.matches();
}
}
- 選權限說明:將下面的代碼添加到AndroidManifest.xml里,如下圖
<!-- 可選權限說明-->
<uses-permission
android:name="android.permission.INTERNET" />
允許應用程序聯網(必須)
<uses-permission
android:name="android.permission.ACCESS_WIFI_STATE" />
允許訪問WiFi網絡狀態信息(必須)
<uses-permission
android:name="android.permission.READ_PHONE_STATE" />
允許讀取手機狀態(必須)
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
允許訪問網絡狀態(必須)
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
允許寫手機存儲(必須)
<uses-permission
android:name="android.permission.RECEIVE_SMS" />
GUI自動填充驗證碼功能(非必須)
<uses-permission
android:name="android.permission.READ_SMS" />
從短信中讀取驗證碼接口(非必須)
<uses-permission
android:name="android.permission.READ_CONTACTS" />
應用內好友功能(非必須)
運行結果:
- 手機號為空,提示輸入手機號
- 手機號格式不正確提示
- 手機號正確輸入
- 驗證碼輸入錯誤
- 驗證碼輸入正確