簡訊驗證(mob)
新人一個,請大家多多指教!
1.自己去Mob官網下載一個簡訊的sdk
2.解壓檔案開啟,有一個SMSSDK的資料夾,開啟後有五個檔案,只複製一下這四個就行(MobCommons-2017.0321.1624.jar,MobTools-2017.0321.1624.jar,
SMSSDK-2.1.4.aar,SMSSDKGUI-2.1.4.aar),(studio)貼上到自己建立的專案下的libs目錄下。
3.將這句代貼上到build.gridle檔案下,自己單獨一個位置
repositories{
flatDir{
dirs 'libs' //就是你放aar的目錄地址
}
}
然後再將這兩句貼上build.gridle檔案的dependencies下
compile name:'SMSSDK-2.1.4',ext:'aar'
compile name:'SMSSDKGUI-2.1.4',ext:'aar'
4.清單檔案中新增許可權
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5.mainactivity介面佈局檔案
<EditText
android:hint="輸入手機號"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_phone"/>
<EditText
android:hint="輸入驗證碼"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_code" />
<Button
android:text="接收驗證碼"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt_getCode"/>
<Button
android:text="傳送驗證碼"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt_verify"/>
6.MainActivity介面程式碼
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import static cn.smssdk.SMSSDK.getSupportedCountries;
import static cn.smssdk.SMSSDK.getVerificationCode;
import static cn.smssdk.SMSSDK.submitVerificationCode;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btnrequest,btnsub;
private EditText etphone,etcode;
int i =60;
Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == -9) {
btnrequest.setText("重新發送(" + i + ")");
} else if (msg.what == -8) {
btnrequest.setText("獲取驗證碼");
btnrequest.setClickable(true); // 設定可點選
i = 30;
} else {
int event = msg.arg1;
int result = msg.arg2;
Object data = msg.obj;
if (result == SMSSDK.RESULT_COMPLETE) {
// 簡訊註冊成功後,返回MainActivity,然後提示
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {// 提交驗證碼成功
Toast.makeText(getApplicationContext(), "提交驗證碼成功",
Toast.LENGTH_SHORT).show();
// 驗證成功後跳轉主介面
//此處可註釋掉
Intent intent = new Intent(MainActivity.this, reg.class);
intent.putExtra("flag", "newuser");
startActivity(intent);
Log.e("註冊", "成功!");
finish();// 成功跳轉之後銷燬當前頁面
} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
Toast.makeText(getApplicationContext(), "驗證碼已經發送",
Toast.LENGTH_SHORT).show();
} else {
((Throwable) data).printStackTrace();
}
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SMSSDK.initSDK(this, "1df6e8fd96cec", "674e88e1e2a68af8a5cbe1551dcb9769");
etphone = (EditText) findViewById(R.id.et_phone);
etcode = (EditText) findViewById(R.id.et_code);
btnrequest = (Button) findViewById(R.id.bt_getCode);
btnsub = (Button) findViewById(R.id.bt_verify);
btnrequest.setOnClickListener(this);
btnsub.setOnClickListener(this);
Log.e("ms", "1");
// 啟動簡訊驗證sdk
SMSSDK.initSDK(this, "186af2e954049",
"48466e82249da303af399acb41f5e703");
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); // 註冊回撥監聽介面
};
@Override
public void onClick(View view) {
String phoneNums = etphone.getText().toString();
switch (view.getId()) {
case R.id.bt_getCode:
if (!judgePhoneNums(phoneNums)) {// 判斷輸入號碼是否正確
return;
}
SMSSDK.getVerificationCode("86", phoneNums); // 呼叫sdk傳送簡訊驗證
btnrequest.setClickable(false);// 設定按鈕不可點選 顯示倒計時
btnrequest.setText("重新發送(" + i + ")");
new Thread(new Runnable() {
@Override
public void run() {
for (i = 30; i > 0; i--) {
handler.sendEmptyMessage(-9);
if (i <= 0) {
break;
}
try {
Thread.sleep(1000);// 執行緒休眠實現讀秒功能
} catch (InterruptedException e) {
e.printStackTrace();
}
}
handler.sendEmptyMessage(-8);// 在30秒後重新顯示為獲取驗證碼
}
}).start();
break;
case R.id.bt_verify:
SMSSDK.submitVerificationCode("86", phoneNums, etcode.getText()
.toString());
// createProgressBar();
break;
}
}
/**
* 判斷手機號碼是否合理
*
* @param phoneNums
*/
private boolean judgePhoneNums(String phoneNums) {
if (isMatchLength(phoneNums, 11) && isMobileNO(phoneNums)) {
return true;
}
Toast.makeText(this, "手機號碼輸入有誤!", Toast.LENGTH_SHORT).show();
return false;
}
public static boolean isMatchLength(String str, int length) {
if (str.isEmpty()) {
return false;
} else {
return str.length() == length ? true : false;
}
}
public static boolean isMobileNO(String mobileNums) {
String telRegex = "[1][3578]\\d{9}";// "[1]"代表第1位為數字1,"[358]"代表第二位可以為3、5、8中的一個,"\\d{9}"代表後面是可以是0~9的數字,有9位。
if (TextUtils.isEmpty(mobileNums))
return false;
else
return mobileNums.matches(telRegex);
}
@Override
protected void onDestroy() {
SMSSDK.unregisterAllEventHandler();
super.onDestroy();
}
}
7.最後別忘了建立一個activity介面,用於跳轉,配置一下