開相關發中總會遇到短信驗證這些操做,這周沒有來得及寫新的東西,藉此分享一篇之前學習短信驗證的筆記,本文使用的是 Mob 提供的 SMSSDK .java
官網下載地址:SMSSDKandroid
將 MobCommons.jar、MobTools.jar、SMSSDK-2.0.1.aar、SMSSDKGUI-2.0.1.aar 放到了app 的 libs 目錄下,若是不須要帶界面的 SMSSDK 能夠不添加 SMSSDKGUI-2.0.1.aar,具體文件請參考最新的 SMSSDK。服務器
打開 app 下面的 build.gradle 文件進行以下配置:微信
在 AndroidManifest.xml 文件中配置權限和Application.app
<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.READ_SMS" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<activity android:name="com.mob.tools.MobUIShell" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="keyboardHidden|orientation|screenSize" android:windowSoftInputMode="stateHidden|adjustResize"/>
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); SMSSDK.initSDK(this, "您的appkey", "您的appsecret"); } }
實現一個簡單的案例,獲取驗證碼,並進行驗證。ide
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.manu.sharesdksms.MainActivity"> <LinearLayout android:id="@+id/ll_user" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="16dp" android:paddingRight="16dp"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:hint="手機號:" /> <EditText android:id="@+id/et_number" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4"/> <TextView android:id="@+id/tv_getCheckCode" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:gravity="center" android:text="獲取驗證碼" android:clickable="true"/> </LinearLayout> <LinearLayout android:id="@+id/ll_pass" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/ll_user" android:paddingLeft="16dp" android:paddingRight="16dp"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="驗證碼:"/> <EditText android:id="@+id/et_checkCode" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4"/> <TextView android:id="@+id/tv_sendCheckCode" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:gravity="center" android:text="驗證" android:clickable="true"/> </LinearLayout> </RelativeLayout>
/** * ShareSDk 驗證碼測試 */ public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private EditText et_number; private EditText et_checkCode; private TextView tv_getCheckCode; private TextView tv_sendCheckCode; private String phoneNumber; private String checkCode; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_number = (EditText) findViewById(R.id.et_number); et_checkCode = (EditText) findViewById(R.id.et_checkCode); tv_getCheckCode = (TextView) findViewById(R.id.tv_getCheckCode); tv_sendCheckCode = (TextView) findViewById(R.id.tv_sendCheckCode); checkCode = et_checkCode.getText().toString().trim(); tv_getCheckCode.setOnClickListener(this); tv_sendCheckCode.setOnClickListener(this); //註冊短信回調 SMSSDK.registerEventHandler(ev); } /** * 短信驗證的回調監聽 */ private EventHandler ev = new EventHandler() { @Override public void afterEvent(int event, int result, Object data) { if (result == SMSSDK.RESULT_COMPLETE) { //回調完成 //提交驗證碼成功,若是驗證成功會在data裏返回數據。data數據類型爲HashMap<number,code> if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { Log.e("TAG", "提交驗證碼成功" + data.toString()); HashMap<String, Object> mData = (HashMap<String, Object>) data; String country = (String) mData.get("country");//返回的國家編號 String phone = (String) mData.get("phone");//返回用戶註冊的手機號 Log.e("TAG", country + "====" + phone); if (phone.equals(phoneNumber)) { runOnUiThread(new Runnable() {//更改ui的操做要放在主線程,實際能夠發送hander @Override public void run() { showDailog("恭喜你!經過驗證"); dialog.dismiss(); } }); } else { runOnUiThread(new Runnable() { @Override public void run() { showDailog("驗證失敗"); dialog.dismiss(); } }); } } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {//獲取驗證碼成功 Log.e("TAG", "獲取驗證碼成功"); } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {//返回支持發送驗證碼的國家列表 } } else { ((Throwable) data).printStackTrace(); } } }; private void showDailog(String text) { new AlertDialog.Builder(this) .setTitle(text) .setPositiveButton("肯定", null) .show(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.tv_getCheckCode: toast("getCode"); getCheckCode(); break; case R.id.tv_sendCheckCode: toast("sendCode"); sendCheckCode(); break; } } /** * 獲取驗證碼 */ public void getCheckCode() { phoneNumber = et_number.getText().toString().trim(); //發送短信,傳入國家號和電話號碼 if (TextUtils.isEmpty(phoneNumber)) { toast("號碼不能爲空!"); } else { SMSSDK.getVerificationCode("+86", phoneNumber); toast("發送成功!"); } } /** * 向服務器提交驗證碼,在監聽回調中監聽是否驗證 */ private void sendCheckCode() { checkCode = et_checkCode.getText().toString(); if (!TextUtils.isEmpty(checkCode)) { dialog = ProgressDialog.show(this, null, "正在驗證...", false, true); //提交短信驗證碼 SMSSDK.submitVerificationCode("+86", phoneNumber, checkCode);//國家號,手機號碼,驗證碼 Toast.makeText(this, "提交了註冊信息:" + phoneNumber, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "驗證碼不能爲空", Toast.LENGTH_SHORT).show(); } } /** * Toast * @param info */ public void toast(String info){ Toast.makeText(MainActivity.this, info, Toast.LENGTH_SHORT).show(); } @Override protected void onDestroy() { SMSSDK.unregisterEventHandler(ev); super.onDestroy(); } }
稍等一下,gif動畫時間有點長,爲了接收到短信哦!佈局
能夠選擇關注微信公衆號:jzman-blog 獲取最新更新,一塊兒交流學習!學習