在發送驗證碼中,後臺咱們使用的是Mob平臺。html
首先註冊成爲Mob平臺用戶,進入後臺,建立短信驗證碼應用,以下圖。java
建立好應用後,記錄App Key值和App Secret值,在初始化SDK時使用。android
下載相應的SDK,解壓後的文件目錄以下,將SMSSDK導入ADT中,設爲庫文件(選擇工程右擊選擇「property」->"Android"選項下的library勾選「Is Library」)。app
配置AndroidManifest.xml文件,添加以下權限:dom
<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" />
<span style="font-size:14px;"><activity android:name="com.mob.tools.MobUIShell" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="keyboardHidden|orientation|screenSize" android:windowSoftInputMode="stateHidden|adjustResize"/></span>
MainActivity.java:
ide
package xyz.baal.testsms; import java.util.HashMap; import java.util.Random; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import cn.smssdk.EventHandler; import xyz.baal.testsms.R; import cn.smssdk.SMSSDK; import cn.smssdk.gui.RegisterPage; public class MainActivity extends Activity { private Button bt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化SMSSDK SMSSDK.initSDK(this, "<您的appkey>", "<您的appsecret>"); bt = (Button) findViewById(R.id.button); bt.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { /* *打開註冊頁面 *短信SDK內置了開源的GUI功能,您能夠經過調用下面的代碼打開短信驗證頁面 */ RegisterPage registerPage = new RegisterPage(); registerPage.setRegisterCallback(new EventHandler() { public void afterEvent(int event, int result, Object data) { // 解析註冊結果 if (result == SMSSDK.RESULT_COMPLETE) { //獲取數據 HashMap<String,Object> phoneMap = (HashMap<String, Object>) data; String country = (String) phoneMap.get("country"); String phone = (String) phoneMap.get("phone"); // 提交用戶信息 registerUser(country, phone); } } }); registerPage.show(MainActivity.this); } }); } public void registerUser(String country,String phone){ Random random = new Random(); String uidString = Math.abs(random.nextInt())+""; String nickName = "BAAL"; SMSSDK.submitUserInfo(uidString, nickName, null, country, phone); } }acticity_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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="短信測試" /> </RelativeLayout>