您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了Android實(shí)現(xiàn)頁(yè)面短信驗(yàn)證功能的具體代碼,供大家參考,具體內(nèi)容如下
目標(biāo)效果:
上一篇博文介紹的是使用SDK中自帶的驗(yàn)證頁(yè)面,需要進(jìn)行多次跳轉(zhuǎn),并且項(xiàng)目需要添加兩個(gè)框架,多有不便,這個(gè)例子簡(jiǎn)化了短信驗(yàn)證的代碼,但是不足的是功能不如原本那個(gè)完善。
1.新建項(xiàng)目,找到上一次下載的SDK文件中包含的SMSSDK項(xiàng)目,復(fù)制lib目錄下的三個(gè)jar包,res目錄下的raw文件夾和values目錄下的兩個(gè)xml文件到本項(xiàng)目。
2.activity_main.xml頁(yè)面設(shè)置布局。
activity_main.xml頁(yè)面:
<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="請(qǐng)輸入手機(jī)號(hào)" 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="請(qǐng)輸入驗(yàn)證碼" 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="獲取驗(yàn)證碼" /> <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="注冊(cè)" /> </RelativeLayout>
3.MainActivity.java頁(yè)面驗(yàn)證信息。
MainActivity.java頁(yè)面:
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應(yīng)用后臺(tái)注冊(cè)得到的APPSECRET private static String APPSECRET ="a881cac45bce10a59ccf22203062e763"; private EditText etPhone; private EditText etNumber; private Button btGet; private Button btRegister; private String phone; private MyCountDownTime myCountDownTime;// 用于驗(yàn)證碼倒計(jì)時(shí) private Handler mHandler;//用于執(zhí)行耗時(shí)操作 @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); // 注冊(cè)短信回調(diào) } 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, "電話號(hào)碼不能為空", Toast.LENGTH_SHORT).show(); } break; case R.id.btRegister: String number = etNumber.getText().toString().trim(); if (!TextUtils.isEmpty(number)) { SMSSDK.submitVerificationCode("86", phone, number);// 驗(yàn)證短信 } else { Toast.makeText(MainActivity.this, "驗(yàn)證碼不能為空", 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 發(fā)送成功后,開始倒計(jì)時(shí) * */ private void startTimer() { if (null == myCountDownTime) { myCountDownTime = new MyCountDownTime(60000, 1000, btGet, "重新發(fā)送"); } myCountDownTime.start(); } /** * * @Description: TODO 一般發(fā)送失敗時(shí),需要重置Button狀態(tài) */ private void cancelTimer(){ if(null != myCountDownTime){ myCountDownTime.cancel(); myCountDownTime.onFinish(); } } protected void onDestroy() { // 銷毀回調(diào)監(jiān)聽接口 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) { // 回調(diào)完成 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { // 提交驗(yàn)證碼成功 Toast.makeText(MainActivity.this, "驗(yàn)證成功", Toast.LENGTH_SHORT) .show(); } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { // 獲取驗(yàn)證碼成功 Toast.makeText(MainActivity.this, "獲取驗(yàn)證碼成功", Toast.LENGTH_SHORT) .show(); } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) { // 返回支持發(fā)送驗(yàn)證碼的國(guó)家列表 } } 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頁(yè)面,用于獲取驗(yàn)證碼后按鈕的倒計(jì)時(shí)。
MyCountDownTime.java頁(yè)面:
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應(yīng)用后臺(tái)注冊(cè)得到的APPSECRET private static String APPSECRET ="a881cac45bce10a59ccf22203062e763"; private EditText etPhone; private EditText etNumber; private Button btGet; private Button btRegister; private String phone; private MyCountDownTime myCountDownTime;// 用于驗(yàn)證碼倒計(jì)時(shí) private Handler mHandler;//用于執(zhí)行耗時(shí)操作 @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); // 注冊(cè)短信回調(diào) } 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, "電話號(hào)碼不能為空", Toast.LENGTH_SHORT).show(); } break; case R.id.btRegister: String number = etNumber.getText().toString().trim(); if (!TextUtils.isEmpty(number)) { SMSSDK.submitVerificationCode("86", phone, number);// 驗(yàn)證短信 } else { Toast.makeText(MainActivity.this, "驗(yàn)證碼不能為空", 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 發(fā)送成功后,開始倒計(jì)時(shí) * */ private void startTimer() { if (null == myCountDownTime) { myCountDownTime = new MyCountDownTime(60000, 1000, btGet, "重新發(fā)送"); } myCountDownTime.start(); } /** * * @Description: TODO 一般發(fā)送失敗時(shí),需要重置Button狀態(tài) */ private void cancelTimer(){ if(null != myCountDownTime){ myCountDownTime.cancel(); myCountDownTime.onFinish(); } } protected void onDestroy() { // 銷毀回調(diào)監(jiān)聽接口 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) { // 回調(diào)完成 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { // 提交驗(yàn)證碼成功 Toast.makeText(MainActivity.this, "驗(yàn)證成功", Toast.LENGTH_SHORT) .show(); } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { // 獲取驗(yàn)證碼成功 Toast.makeText(MainActivity.this, "獲取驗(yàn)證碼成功", Toast.LENGTH_SHORT) .show(); } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) { // 返回支持發(fā)送驗(yàn)證碼的國(guó)家列表 } } 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.運(yùn)行就可以顯示目標(biāo)效果了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。