Android短信验证码SDK
1 登陆mob.com官网下载短信验证码sdk
1.1 登陆mob.com注册一个账户,进入后台
1.2 下载对应的短信验证码sdk
1.3 解压短信验证码sdk之后,可以看到的目录结构
1.4 添加应用 APPKEY和APPSECRET将在代码中使用到
2 Eclipse开发—依赖项目
2.1 将短信验证码的sdk中的SMSSDK导入到eclipse中
ShortMessageSDKGUI项目中依赖SMSSDK项目
SMSProject依赖 ShortMessageSDKGUI项目
其中SMSProjec是我们需要建的项目,ShortMessageSDKGUI和SMSSDK项目都是通过mob.com提供的项目,只需要导入到eclipse中就可以
2.2 在导入短信验证码的sdk中的SMSSDK可能遇到的错误
2.2.1 [2016-07-13 09:18:49 -ShortMessageSDKGUI] Unable to resolve target 'android-23'
需要修改project.properties中的
在target=android-xx,你的sdk版本,如果不清楚自己的sdk版本,可以查看新建的项目的project.properties文件
2.2.2 ShortMessageSDKGUI可能出现乱码问题,修改编码方式
选中项目右键-》properties-》resource
2.3 将ShortMessageSDKGUI选中为library
2.4 新建一个项目,这个才是我们自己建的项目
2.4.1.1在自己的新建的项目中,即SSMProject添加依赖项目
2.4.1.2Main_activity.xml布局文件
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_bind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="@string/btn_blind_text"
/>
</LinearLayout>
2.4.1.3Success.xml布局文件
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="success"
/>
</LinearLayout>
2.4.1.4 MainActivity ,需要注意的是APPKEY和APPSECRET是我们在mob.com注册的账号进入后台的时候,页面上的信息
package com.hzs.activity;
import java.util.HashMap;
import java.util.Random;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;
import com.hzs.smsproject.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity{
//声明button
privateButton btn_blind;
//声明APPKEY
privateString APPKEY="14d755a9d70f0";
//声明APPSECRET
privateString APPSECRET="031e5b58d4b76c4915e96de0a48fb5ab";
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main_activity);
init();
}
publicvoid init(){
//1.初始化
SMSSDK.initSDK(this,APPKEY, APPSECRET);
//2.在xml中配置信息,权限+activity
//获得控件
FindViewById();
//为控件添加点击事件
addOnClickListener();
}
publicvoid FindViewById(){
btn_blind=(Button)this.findViewById(R.id.btn_bind);
}
publicvoid addOnClickListener(){
btn_blind.setOnClickListener(newmyClickListener());
}
publicvoid submitUserInfo(String country,String phone){
Randomr=new Random();
Stringuid=Math.abs(r.nextInt())+"";
Stringname="hzs";
SMSSDK.submitUserInfo(uid,name, null, country, phone);
}
publicclass myClickListener implements OnClickListener{
@Override
publicvoid onClick(View arg0) {
switch(arg0.getId()) {
caseR.id.btn_bind:
//注册用户页面
RegisterPageregisterPage=new RegisterPage();
//注册回调方法
registerPage.setRegisterCallback(newEventHandler(){
//方法完成后执行的方法
@Override
publicvoid afterEvent(int arg0, int arg1, Object arg2) {
super.afterEvent(arg0,arg1, arg2);
//判断是否
if(arg1==SMSSDK.RESULT_COMPLETE){
HashMap<String,Object> maps=(HashMap<String, Object>) arg2;
//获取国家
Stringcountry=(String) maps.get("country");
//获取电话号码
Stringphone=(String) maps.get("phone");
//提交用户信息
submitUserInfo(country,phone);
//验证成功后跳的界面
Intentintent=new Intent(MainActivity.this,SuccessActivity.class);
startActivity(intent);
}
}
});
//显示注册页面
registerPage.show(MainActivity.this);
break;
default:
break;
}
}
}
}