SMSSDK提供了手机短信验证码的功能,而且是免费的,若app没有在Mob中提交审核,那么一天最多发送条短信;若该手机号验证成功,那么将不再发送短信(这是因为Mob默认开启了智能验证,关闭即可(后台中心–>短信设置–>智能验证,见下图))
Android 短信SDK集成文档
解压下载的SDK,把3个jar + 1个aar放入到libs中,添加完成后,还需要在Module中配置aar
解压后:
添加jar和aar
repositories {
flatDir {
dirs 'libs'//就是你放aar的目录地址
}
}
dependencies {
...
compile name: 'SMSSDK-2.1.2', ext: 'aar'
compile name: 'SMSSDKGUI-2.1.2', ext: 'aar'
}
dependencies 中有compile fileTree(dir: 'libs', include: ['*.jar'])
可以不用重复在dependencies中
compile files('libs/MobCommons-2016.1012.1447.jar')
compile files('libs/MobTools-2016.1012.1447.jar')
有下面3个即可
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile name: 'SMSSDK-2.1.2', ext: 'aar'
compile name: 'SMSSDKGUI-2.1.2', ext: 'aar'
}
配置网络权限
<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节点(不需要SMSSDKGUI-2.1.2.aar就可以不配置)
<activity
android:name="com.mob.tools.MobUIShell"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="stateHidden|adjustResize"/>
最好在Application中设置
//初始化SMSSDK
SMSSDK.initSDK(this,"Mob官网注册的key","Mob官网注册的secret");
//打开注册页面,这个页面就是我们在AndroidManifest.xml中添加的节点com.mob.tools.MobUIShell
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");
Log.d(TAG, "opeRegisterPager()--country=" + country + "--phone" + phone);
}
}
});
registerPage.show(context);
前三步是一样的
先注册回调
//注册回调
SMSSDK.registerEventHandler(eventHandler);
private EventHandler eventHandler = new EventHandler() {
@Override
public void afterEvent(int event, int result, Object data) {
super.afterEvent(event, result, data);
if (result == SMSSDK.RESULT_COMPLETE) {
//回调完成
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
//提交验证码成功
@SuppressWarnings("unchecked") HashMap<String, Object> phoneMap = (HashMap<String, Object>) data;
String country = (String) phoneMap.get("country");
String phone = (String) phoneMap.get("phone");
Log.d(TAG, "提交验证码成功--country=" + country + "--phone" + phone);
} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
//获取验证码成功
Log.d(TAG, "获取验证码成功");
} else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
//返回支持发送验证码的国家列表
}
} else {
((Throwable) data).printStackTrace();
}
}
};
获取验证码
SMSSDK.getVerificationCode("86", phone);
都可以
SMSSDK.getVerificationCode("+86", phone);
发送验证码 code:验证码
SMSSDK.submitVerificationCode("+86", phone, code);
//打开通信录好友列表页面
ContactsPage contactsPage = new ContactsPage();
contactsPage.show(context);
SMSSDKDemo02
Jpush也支持短信验证码,但是需要申请客服批准,这里就不说了。