您好,登錄后才能下訂單哦!
本文實例為大家分享了Android實現頁面短信驗證功能的具體代碼,供大家參考,具體內容如下
目標效果:
上一篇博文介紹的是使用SDK中自帶的驗證頁面,需要進行多次跳轉,并且項目需要添加兩個框架,多有不便,這個例子簡化了短信驗證的代碼,但是不足的是功能不如原本那個完善。
1.新建項目,找到上一次下載的SDK文件中包含的SMSSDK項目,復制lib目錄下的三個jar包,res目錄下的raw文件夾和values目錄下的兩個xml文件到本項目。
2.activity_main.xml頁面設置布局。
activity_main.xml頁面:
<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" > <EditText android:id="@+id/etPhone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:hint="請輸入手機號" android:layout_marginTop="45dp" android:ems="10" > <requestFocus /> </EditText> <EditText android:id="@+id/etNumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/etPhone" android:hint="請輸入驗證碼" android:layout_marginTop="26dp" android:ems="10" /> <Button android:id="@+id/btGet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/etNumber" android:layout_alignParentRight="true" android:text="獲取驗證碼" /> <Button android:id="@+id/btRegister" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="注冊" /> </RelativeLayout>
3.MainActivity.java頁面驗證信息。
MainActivity.java頁面:
package com.example.notetest; import org.json.JSONObject; import com.example.notetest.MyCountDownTime; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Handler.Callback; import android.os.Message; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import cn.smssdk.EventHandler; import cn.smssdk.SMSSDK; import cn.smssdk.utils.SMSLog; public class MainActivity extends Activity implements OnClickListener, Callback { private static String APPKEY= "13e9653a7ff00"; // 填寫從短信SDK應用后臺注冊得到的APPSECRET private static String APPSECRET ="a881cac45bce10a59ccf22203062e763"; private EditText etPhone; private EditText etNumber; private Button btGet; private Button btRegister; private String phone; private MyCountDownTime myCountDownTime;// 用于驗證碼倒計時 private Handler mHandler;//用于執行耗時操作 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SMSSDK.initSDK(this, APPKEY, APPSECRET, false); btGet = (Button) findViewById(R.id.btGet); btRegister = (Button) findViewById(R.id.btRegister); etPhone = (EditText) findViewById(R.id.etPhone); etNumber = (EditText) findViewById(R.id.etNumber); btGet.setOnClickListener(this); btRegister.setOnClickListener(this); initSDK(); } private void initSDK() { try { final Handler handler = new Handler(this); EventHandler eventHandler = new EventHandler() { 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); // 注冊短信回調 } catch (Exception e) { e.printStackTrace(); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btGet: phone = etPhone.getText().toString().trim(); if (!TextUtils.isEmpty(phone)) { SMSSDK.getVerificationCode("86", phone);// 獲取短信 getCode(); } else { Toast.makeText(MainActivity.this, "電話號碼不能為空", Toast.LENGTH_SHORT).show(); } break; case R.id.btRegister: String number = etNumber.getText().toString().trim(); if (!TextUtils.isEmpty(number)) { SMSSDK.submitVerificationCode("86", phone, number);// 驗證短信 } else { Toast.makeText(MainActivity.this, "驗證碼不能為空", Toast.LENGTH_SHORT) .show(); return; } break; default: break; } } private void getCode() { mHandler = new Handler(); mHandler.postDelayed(new Runnable() { @Override public void run() { startTimer(); } }, 2000); } /** * * @Description: TODO 發送成功后,開始倒計時 * */ private void startTimer() { if (null == myCountDownTime) { myCountDownTime = new MyCountDownTime(60000, 1000, btGet, "重新發送"); } myCountDownTime.start(); } /** * * @Description: TODO 一般發送失敗時,需要重置Button狀態 */ private void cancelTimer(){ if(null != myCountDownTime){ myCountDownTime.cancel(); myCountDownTime.onFinish(); } } protected void onDestroy() { // 銷毀回調監聽接口 SMSSDK.unregisterAllEventHandler(); super.onDestroy(); } @Override public boolean handleMessage(Message msg) { int event = msg.arg1; int result = msg.arg2; Object data = msg.obj; if (result == SMSSDK.RESULT_COMPLETE) { // 回調完成 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { // 提交驗證碼成功 Toast.makeText(MainActivity.this, "驗證成功", Toast.LENGTH_SHORT) .show(); } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { // 獲取驗證碼成功 Toast.makeText(MainActivity.this, "獲取驗證碼成功", Toast.LENGTH_SHORT) .show(); } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) { // 返回支持發送驗證碼的國家列表 } } else { int status = 0; try { ((Throwable) data).printStackTrace(); Throwable throwable = (Throwable) data; JSONObject object = new JSONObject(throwable.getMessage()); String des = object.optString("detail"); status = object.optInt("status"); if (!TextUtils.isEmpty(des)) { Toast.makeText(MainActivity.this, des, Toast.LENGTH_SHORT) .show(); return false; } } catch (Exception e) { SMSLog.getInstance().w(e); } } return false; } }
4.新建MyCountDownTime.java頁面,用于獲取驗證碼后按鈕的倒計時。
MyCountDownTime.java頁面:
package com.example.notetest; import org.json.JSONObject; import com.example.notetest.MyCountDownTime; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Handler.Callback; import android.os.Message; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import cn.smssdk.EventHandler; import cn.smssdk.SMSSDK; import cn.smssdk.utils.SMSLog; public class MainActivity extends Activity implements OnClickListener, Callback { private static String APPKEY= "13e9653a7ff00"; // 填寫從短信SDK應用后臺注冊得到的APPSECRET private static String APPSECRET ="a881cac45bce10a59ccf22203062e763"; private EditText etPhone; private EditText etNumber; private Button btGet; private Button btRegister; private String phone; private MyCountDownTime myCountDownTime;// 用于驗證碼倒計時 private Handler mHandler;//用于執行耗時操作 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SMSSDK.initSDK(this, APPKEY, APPSECRET, false); btGet = (Button) findViewById(R.id.btGet); btRegister = (Button) findViewById(R.id.btRegister); etPhone = (EditText) findViewById(R.id.etPhone); etNumber = (EditText) findViewById(R.id.etNumber); btGet.setOnClickListener(this); btRegister.setOnClickListener(this); initSDK(); } private void initSDK() { try { final Handler handler = new Handler(this); EventHandler eventHandler = new EventHandler() { 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); // 注冊短信回調 } catch (Exception e) { e.printStackTrace(); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btGet: phone = etPhone.getText().toString().trim(); if (!TextUtils.isEmpty(phone)) { SMSSDK.getVerificationCode("86", phone);// 獲取短信 getCode(); } else { Toast.makeText(MainActivity.this, "電話號碼不能為空", Toast.LENGTH_SHORT).show(); } break; case R.id.btRegister: String number = etNumber.getText().toString().trim(); if (!TextUtils.isEmpty(number)) { SMSSDK.submitVerificationCode("86", phone, number);// 驗證短信 } else { Toast.makeText(MainActivity.this, "驗證碼不能為空", Toast.LENGTH_SHORT) .show(); return; } break; default: break; } } private void getCode() { mHandler = new Handler(); mHandler.postDelayed(new Runnable() { @Override public void run() { startTimer(); } }, 2000); } /** * * @Description: TODO 發送成功后,開始倒計時 * */ private void startTimer() { if (null == myCountDownTime) { myCountDownTime = new MyCountDownTime(60000, 1000, btGet, "重新發送"); } myCountDownTime.start(); } /** * * @Description: TODO 一般發送失敗時,需要重置Button狀態 */ private void cancelTimer(){ if(null != myCountDownTime){ myCountDownTime.cancel(); myCountDownTime.onFinish(); } } protected void onDestroy() { // 銷毀回調監聽接口 SMSSDK.unregisterAllEventHandler(); super.onDestroy(); } @Override public boolean handleMessage(Message msg) { int event = msg.arg1; int result = msg.arg2; Object data = msg.obj; if (result == SMSSDK.RESULT_COMPLETE) { // 回調完成 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { // 提交驗證碼成功 Toast.makeText(MainActivity.this, "驗證成功", Toast.LENGTH_SHORT) .show(); } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { // 獲取驗證碼成功 Toast.makeText(MainActivity.this, "獲取驗證碼成功", Toast.LENGTH_SHORT) .show(); } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) { // 返回支持發送驗證碼的國家列表 } } else { int status = 0; try { ((Throwable) data).printStackTrace(); Throwable throwable = (Throwable) data; JSONObject object = new JSONObject(throwable.getMessage()); String des = object.optString("detail"); status = object.optInt("status"); if (!TextUtils.isEmpty(des)) { Toast.makeText(MainActivity.this, des, Toast.LENGTH_SHORT) .show(); return false; } } catch (Exception e) { SMSLog.getInstance().w(e); } } return false; } }
5.運行就可以顯示目標效果了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。