Android獲取短信驗(yàn)證碼的實(shí)現(xiàn)方法
先給大家展示下效果圖,如果感覺(jué)不錯(cuò),請(qǐng)參考實(shí)現(xiàn)思路詳解

Android開(kāi)發(fā)中關(guān)于短息驗(yàn)證碼的設(shè)計(jì)層出不窮,越來(lái)越多的應(yīng)用為了更好的提高軟件的安全性,開(kāi)始使用通過(guò)服務(wù)器向用戶(hù)發(fā)送驗(yàn)證碼的方式,來(lái)保護(hù)用戶(hù)個(gè)人信息的安全性。無(wú)論是用戶(hù)注冊(cè)時(shí)的信息驗(yàn)證還是當(dāng)用戶(hù)發(fā)出找回密碼請(qǐng)求時(shí)的短信驗(yàn)證,他們的工作原理大致上是一致的,因?yàn)轫?xiàng)目的需要研究了一下關(guān)于這方面的知識(shí),本篇我將帶領(lǐng)大家一起實(shí)現(xiàn)這一當(dāng)下流行的設(shè)計(jì)方案。
眾所周知,短信驗(yàn)證需要服務(wù)器端生成一個(gè)驗(yàn)證碼,然后發(fā)送到用戶(hù)輸入的手機(jī)上,這個(gè)過(guò)程需要服務(wù)器主動(dòng)向客戶(hù)發(fā)送驗(yàn)證短信,所以這是就需要一個(gè)移動(dòng)或聯(lián)通的發(fā)送短息接口,由于本人目前尚處于學(xué)生階段,沒(méi)有獲得這個(gè)接口的權(quán)限,所以我就選擇了借助網(wǎng)上的移動(dòng)開(kāi)發(fā)服務(wù)平臺(tái),來(lái)完成這個(gè)功能的實(shí)現(xiàn),這里我借用的平臺(tái)是:http://dashboard.mob.com/,大家可以關(guān)注一下,這個(gè)平臺(tái)為我們開(kāi)發(fā)移動(dòng)應(yīng)用提供了很好的技術(shù)指導(dǎo),可以大大縮短我們的開(kāi)發(fā)周期。廢話(huà)不多說(shuō),下面開(kāi)始我們今天的重點(diǎn)。
官方為我們提供了兩種設(shè)計(jì)方式:第一種調(diào)用內(nèi)部GUI實(shí)現(xiàn);另一種通過(guò)自定義GUI實(shí)現(xiàn),對(duì)于第一種方式,我就不再多講,因?yàn)楣俜轿臋n提供了很詳細(xì)的實(shí)行步驟,大家只需要按照上面的步驟去實(shí)現(xiàn)即可,沒(méi)有難度。本篇我將帶領(lǐng)大家通過(guò)自定義GUI實(shí)現(xiàn)短信驗(yàn)證功能。首先開(kāi)發(fā)之前你可以先查閱一下官方提供的無(wú)GUI API,然后下載一下官方提供的dome,做好這些工作之后,我們就可以開(kāi)始我們的設(shè)計(jì)了。
1、將demo中的libs下的SMSSDK-1.1.5.jar和armeabi文件夾拷貝到我們項(xiàng)目的libs目錄下,這是官方提供的類(lèi)庫(kù)jar包。
2、在AndroidManifest.xml文件添加權(quán)限和聲明Action:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_sms" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <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.RECEIVE_SMS" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.android_sms.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="cn.smssdk.SMSSDKUIShell" android:configChanges="keyboardHidden|orientation|screenSize" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:windowSoftInputMode="stateHidden|adjustResize" /> </application> </manifest>
3、設(shè)計(jì)我們的布局文件:
<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" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:text="短信驗(yàn)證" android:textColor="#00ffaa" android:textSize="20dp" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView2" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:text="手機(jī)號(hào):" /> <EditText android:id="@+id/phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView1" android:layout_alignBottom="@+id/textView1" android:layout_toRightOf="@+id/textView1" android:maxLength="11" android:ems="11" android:inputType="phone" > <requestFocus /> </EditText> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_marginTop="40dp" android:layout_below="@+id/phone" android:text="驗(yàn)證碼:"/> <EditText android:id="@+id/cord" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_alignBaseline="@+id/textView3" android:layout_alignBottom="@+id/textView3" android:layout_alignLeft="@+id/phone" android:ems="4" android:maxLength="4" android:inputType="phone" /> <Button android:id="@+id/getcord" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/cord" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_toRightOf="@+id/cord" android:visibility="visible" android:text="獲取驗(yàn)證碼" /> <Button android:id="@+id/savecord" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/cord" android:layout_margin="20dp" android:text="驗(yàn)證" /> <TextView android:id="@+id/now" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/savecord" android:layout_toRightOf="@+id/cord" android:gravity="center_horizontal" android:visibility="gone" android:text="提示信息" android:textColor="#aaaaaa" /> </RelativeLayout>
4、我們的MainActivity:
/**
* 自定義GUI短信驗(yàn)證
* @time: 2015年7月4日
*/
public class MainActivity extends Activity implements OnClickListener{
private EditText phone;
private EditText cord;
private TextView now;
private Button getCord;
private Button saveCord;
private String iPhone;
private String iCord;
private int time = 60;
private boolean flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
init();
SMSSDK.initSDK(this, "<您的appkey>", "<您的appsecret>");
EventHandler eh=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(eh);
}
private void init() {
phone = (EditText) findViewById(R.id.phone);
cord = (EditText) findViewById(R.id.cord);
now = (TextView) findViewById(R.id.now);
getCord = (Button) findViewById(R.id.getcord);
saveCord = (Button) findViewById(R.id.savecord);
getCord.setOnClickListener(this);
saveCord.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.getcord:
if(!TextUtils.isEmpty(phone.getText().toString().trim())){
if(phone.getText().toString().trim().length()==11){
iPhone = phone.getText().toString().trim();
SMSSDK.getVerificationCode("86",iPhone);
cord.requestFocus();
getCord.setVisibility(View.GONE);
}else{
Toast.makeText(MainActivity.this, "請(qǐng)輸入完整電話(huà)號(hào)碼", Toast.LENGTH_LONG).show();
phone.requestFocus();
}
}else{
Toast.makeText(MainActivity.this, "請(qǐng)輸入您的電話(huà)號(hào)碼", Toast.LENGTH_LONG).show();
phone.requestFocus();
}
break;
case R.id.savecord:
if(!TextUtils.isEmpty(cord.getText().toString().trim())){
if(cord.getText().toString().trim().length()==4){
iCord = cord.getText().toString().trim();
SMSSDK.submitVerificationCode("86", iPhone, iCord);
flag = false;
}else{
Toast.makeText(MainActivity.this, "請(qǐng)輸入完整驗(yàn)證碼", Toast.LENGTH_LONG).show();
cord.requestFocus();
}
}else{
Toast.makeText(MainActivity.this, "請(qǐng)輸入驗(yàn)證碼", Toast.LENGTH_LONG).show();
cord.requestFocus();
}
break;
default:
break;
}
}
//驗(yàn)證碼送成功后提示文字
private void reminderText() {
now.setVisibility(View.VISIBLE);
handlerText.sendEmptyMessageDelayed(1, 1000);
}
Handler handlerText =new Handler(){
public void handleMessage(Message msg) {
if(msg.what==1){
if(time>0){
now.setText("驗(yàn)證碼已發(fā)送"+time+"秒");
time--;
handlerText.sendEmptyMessageDelayed(1, 1000);
}else{
now.setText("提示信息");
time = 60;
now.setVisibility(View.GONE);
getCord.setVisibility(View.VISIBLE);
}
}else{
cord.setText("");
now.setText("提示信息");
time = 60;
now.setVisibility(View.GONE);
getCord.setVisibility(View.VISIBLE);
}
};
};
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
int event = msg.arg1;
int result = msg.arg2;
Object data = msg.obj;
Log.e("event", "event="+event);
if (result == SMSSDK.RESULT_COMPLETE) {
//短信注冊(cè)成功后,返回MainActivity,然后提示新好友
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {//提交驗(yàn)證碼成功,驗(yàn)證通過(guò)
Toast.makeText(getApplicationContext(), "驗(yàn)證碼校驗(yàn)成功", Toast.LENGTH_SHORT).show();
handlerText.sendEmptyMessage(2);
} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){//服務(wù)器驗(yàn)證碼發(fā)送成功
reminderText();
Toast.makeText(getApplicationContext(), "驗(yàn)證碼已經(jīng)發(fā)送", Toast.LENGTH_SHORT).show();
}else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){//返回支持發(fā)送驗(yàn)證碼的國(guó)家列表
Toast.makeText(getApplicationContext(), "獲取國(guó)家列表成功", Toast.LENGTH_SHORT).show();
}
} else {
if(flag){
getCord.setVisibility(View.VISIBLE);
Toast.makeText(MainActivity.this, "驗(yàn)證碼獲取失敗,請(qǐng)重新獲取", Toast.LENGTH_SHORT).show();
phone.requestFocus();
}else{
((Throwable) data).printStackTrace();
int resId = getStringRes(MainActivity.this, "smssdk_network_error");
Toast.makeText(MainActivity.this, "驗(yàn)證碼錯(cuò)誤", Toast.LENGTH_SHORT).show();
cord.selectAll();
if (resId > 0) {
Toast.makeText(MainActivity.this, resId, Toast.LENGTH_SHORT).show();
}
}
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
SMSSDK.unregisterAllEventHandler();
}
}
注:appkey和appsecret:在http://dashboard.mob.com/注冊(cè)一個(gè)賬號(hào)后,創(chuàng)建一個(gè)發(fā)送短信的應(yīng)用,系統(tǒng)會(huì)自動(dòng)為生成appkey和appsecret
handlerText是我自定義設(shè)計(jì)的Handker對(duì)象,用于當(dāng)服務(wù)器發(fā)送驗(yàn)證碼后,提醒用戶(hù)注意。
以上所述是小編給大家介紹的Android獲取短信驗(yàn)證碼的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android如何通過(guò)手機(jī)獲取驗(yàn)證碼來(lái)完成注冊(cè)功能
- Android開(kāi)發(fā)中通過(guò)手機(jī)號(hào)+短信驗(yàn)證碼登錄的實(shí)例代碼
- Android開(kāi)發(fā)工程中集成mob短信驗(yàn)證碼功能的方法
- Android實(shí)現(xiàn)短信驗(yàn)證碼獲取自動(dòng)填寫(xiě)功能(詳細(xì)版)
- Android獲取和讀取短信驗(yàn)證碼的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)自動(dòng)提取短信驗(yàn)證碼功能
- Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)填寫(xiě)功能
- Android實(shí)現(xiàn)常見(jiàn)的驗(yàn)證碼輸入框?qū)嵗a
- Android自定義控件通用驗(yàn)證碼輸入框的實(shí)現(xiàn)
- Android實(shí)現(xiàn)隨機(jī)生成驗(yàn)證碼
相關(guān)文章
Android Studio如何快速導(dǎo)入jar和.so文件
這篇文章主要介紹了Android Studio如何快速導(dǎo)入jar和.so文件的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12
基于Android實(shí)現(xiàn)答題倒計(jì)時(shí)功能
這篇文章主要介紹了基于Android實(shí)現(xiàn)答題倒計(jì)時(shí)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Android EditText限制輸入字?jǐn)?shù)的方法
這篇文章主要介紹了Android EditText限制輸入字?jǐn)?shù)的方法,涉及Android針對(duì)EditText文本與字符串操作相關(guān)技巧,需要的朋友可以參考下2016-01-01
如何用HMS Nearby Service給自己的App添加近距離數(shù)據(jù)傳輸功能
這篇文章主要介紹了如何用HMS Nearby Service給自己的App添加近距離數(shù)據(jù)傳輸功能,本文通過(guò)圖文示例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Android OkHttp實(shí)現(xiàn)全局過(guò)期token自動(dòng)刷新示例
本篇文章主要介紹了Android OkHttp實(shí)現(xiàn)全局過(guò)期token自動(dòng)刷新示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Android使用WebView實(shí)現(xiàn)全屏切換播放網(wǎng)頁(yè)視頻功能
這篇文章主要介紹了Android使用WebView實(shí)現(xiàn)全屏切換播放網(wǎng)頁(yè)視頻功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-07-07
Android動(dòng)畫(huà)學(xué)習(xí)筆記之補(bǔ)間動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了Android動(dòng)畫(huà)學(xué)習(xí)筆記之補(bǔ)間動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
react native中的聊天氣泡及timer封裝成的發(fā)送驗(yàn)證碼倒計(jì)時(shí)
這篇文章主要介紹了react native中的聊天氣泡及timer封裝成的發(fā)送驗(yàn)證碼倒計(jì)時(shí)的相關(guān)資料,需要的朋友可以參考下2017-08-08
基于Android實(shí)現(xiàn)保存圖片到本地并可以在相冊(cè)中顯示出來(lái)
App應(yīng)用越來(lái)越人性化,不僅界面優(yōu)美而且服務(wù)也很多樣化,操作也非常方便。通過(guò)本篇文章給大家介紹基于Android實(shí)現(xiàn)保存圖片到本地并可以在相冊(cè)中顯示出來(lái),對(duì)android保存圖片相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-12-12

