Mob官网提供了短信验证的开放平台,通过官方文档和摸索,终于也获取到了短信验证码:现将完整过程展示下:
1.进入官网http://www.mob.com/#/index ,选择SDK下载==>SMS for Android,点击右上角SDK下载
2.解压SDK, 将里面的文件夹SMSSDK(作为Library)和 SMSSDKSample分别导入到eclipse中。导入之后,发现工程出现红色的感叹号,后将这两个项目的版本都调整为API18,感叹号自动消失了。
3.将SMSSDK 引入到自己的工程项目中。这样,整个项目就基本上被部署好了。
4.返回到官网,创建短信验证的项目,获取到App Key 和 App Secret
5.所有的准备工作都做好之后,开始运行SMSSDKSample,
界面展示如下:
此界面弹出的对话框我们可以不用去实现,所以直接找Demonstration of register or binding mobile 这个button 中的实现代码,看看里面的业务逻辑
6 以下是查找到sample中的业务逻辑,将对应的实现功能拷贝到自己的项目中.
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_bind_phone:
// 打开注册页面
RegisterPage registerPage = new RegisterPage();
registerPage.setRegisterCallback(new EventHandler() {
public void afterEvent(int event, int result, Object data) {
// 解析注册结果
if (result == SMSSDK.RESULT_COMPLETE) {
@SuppressWarnings("unchecked")
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(this);
break;
7.整合好之后,自己项目中的代码如下:
public class MainActivity extends Activity {
private Button button;
private static final String[] AVATARS = {
"http://tupian.qqjay.com/u/2011/0729/e755c434c91fed9f6f73152731788cb3.jpg",
"http://99touxiang.com/public/upload/nvsheng/125/27-011820_433.jpg",
"http://img1.touxiang.cn/uploads/allimg/111029/2330264224-36.png",
"http://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339485237265.jpg",
"http://diy.qqjay.com/u/files/2012/0523/f466c38e1c6c99ee2d6cd7746207a97a.jpg",
"http://img1.touxiang.cn/uploads/20121224/24-054837_708.jpg",
"http://img1.touxiang.cn/uploads/20121212/12-060125_658.jpg",
"http://img1.touxiang.cn/uploads/20130608/08-054059_703.jpg",
"http://diy.qqjay.com/u2/2013/0422/fadc08459b1ef5fc1ea6b5b8d22e44b4.jpg",
"http://img1.2345.com/duoteimg/qqTxImg/2012/04/09/13339510584349.jpg",
"http://img1.touxiang.cn/uploads/20130515/15-080722_514.jpg",
"http://diy.qqjay.com/u2/2013/0401/4355c29b30d295b26da6f242a65bcaad.jpg"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn_bind);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
RegisterPage registerPage = new RegisterPage();
registerPage.setRegisterCallback(new EventHandler() {
public void afterEvent(int event, int result, Object data) {
// 解析注册结果
if (result == SMSSDK.RESULT_COMPLETE) {
@SuppressWarnings("unchecked")
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(getApplicationContext());
}
});
SMSSDK.initSDK(this, "自己的App Key", "自己的App Secret");
}
private void registerUser(String country, String phone) {
Random rnd = new Random();
int id = Math.abs(rnd.nextInt());
String uid = String.valueOf(id);
String nickName = "SmsSDK_User_" + uid;
String avatar = AVATARS[id % 12];
SMSSDK.submitUserInfo(uid, nickName, avatar, country, phone);
}
}
8.参照官网的Android 短信SDK集成文档,配置AndroidManifest.xml
添加权限:
<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" />
添加activity:
<activity android:name="com.mob.tools.MobUIShell" android:configChanges="keyboardHidden|orientation|screenSize" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:windowSoftInputMode="stateHidden|adjustResize" >
</activity>