短信验证功能是比较常见的开发功能(使用的IDE为eclipse)。
这边我利用Mob.com的第三方框架来完成短信验证功能。
主要流程:
1,服务端:在www.mob.com添加应用(获得appKey,appSecret);
2,客户端:(1)下载sdk,在eclipse中导入项目:SMSSDK(在导入过程中勾选copy projects into workspace);
(2)选择项目SMSSDK的properties——Android——勾选isLibrary(使其可以作为第三方的类库)
(3)选择自己的项目的properties——Android——Library处add(添加SMSSDK作为第三方类型)
(4)添加对应的权限和注册第三方的Activity(SMSSDK_User_Guide文件中有介绍)
(5)编写代码。
1,服务端创建应用后界面:
2,将项目导入后(代码部分):
package com.example.coolwf;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import java.util.HashMap;
import java.util.Random;
public class MainActivity extends Activity {
private Context mContext;
public static final String appKey ="b509c8*****";
public static final String appSecret = "66edc447541ff376c7**********";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
// 初始化sdk
initSDK();
}
/**
* 初始化短信验证的SDK
*/
private void initSDK() {
SMSSDK.initSDK(mContext, appKey, appSecret);
}
public void register(View view) {
RegisterPage mRegisterPage = new RegisterPage();
mRegisterPage.setRegisterCallback(new EventHandler(){
@Override
public void afterEvent(int event, int result, Object data) {
//解析注册结果
if(result == SMSSDK.RESULT_COMPLETE){
HashMap<String, Object> maps = (HashMap<String, Object>) data;
String country = (String) maps.get("country");
String phone = (String) maps.get("phone");
//提交用户信息
registerUser(country, phone);
}
}
});
mRegisterPage.show(mContext);
}
// 提交用户信息
private void registerUser(String country, String phone){
String uid = Math.abs(new Random().nextInt(100))+"";//用户id
String nickName ="隔壁老王"; //用户昵称
SMSSDK.submitUserInfo(uid, nickName, null, country, phone);
}
}
以上的是利用第三提供的短信验证界面的快速集成,如果你想自定义界面。将上面2.2和2.3改成:将SMSSDK项目下的libs文件夹的jar文件和.so文件复制
到自己项目中对应的文件夹,调用它的api:
package com.example.smsdemo;
import static cn.smssdk.framework.utils.R.getStringRes;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button sensmsButton,verificationButton,countryButton;
private TextView countryTextView,textView2;
private EditText phonEditText,verEditText;
// 填写从短信SDK应用后台注册得到的APPKEY
private static String APPKEY = "5887b8134af8";//463db7238681 27fe7909f8e8
// 填写从短信SDK应用后台注册得到的APPSECRET
private static String APPSECRET = "9cd46d6cf9908a297fbafeb75b7b19d3";//
public String phString; //3684fd4f0e16d68f69645af1c7f8f5bd
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensmsButton=(Button) findViewById(R.id.btn_getVerification_code);
verificationButton=(Button) findViewById(R.id.btn_verification);
countryButton=(Button) findViewById(R.id.btn_supporst_country);
countryTextView=(TextView) findViewById(R.id.textView1);
textView2=(TextView) findViewById(R.id.textView2);
phonEditText=(EditText) findViewById(R.id.et_phone);
verEditText=(EditText) findViewById(R.id.et_verification_code);
sensmsButton.setOnClickListener(this);
verificationButton.setOnClickListener(this);
countryButton.setOnClickListener(this);
//System.loadLibrary("OSbase");
SMSSDK.initSDK(this,APPKEY,APPSECRET);
EventHandler eh=new EventHandler(){
@Override
public void afterEvent(int event, int result, Object data) {
Message msg = new Message();
msg.arg1 = event;
msg.arg2 = result;
msg.obj = data;
handler.sendMessage(msg);
}
};
SMSSDK.registerEventHandler(eh);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_getVerification_code://获取验证码
if(!TextUtils.isEmpty(phonEditText.getText().toString())){
SMSSDK.getVerificationCode("86",phonEditText.getText().toString());
phString=phonEditText.getText().toString();
}else {
Toast.makeText(this, "电话不能为空", 1).show();
}
break;
case R.id.btn_verification://校验验证码
if(!TextUtils.isEmpty(verEditText.getText().toString())){
SMSSDK.submitVerificationCode("86", phString, verEditText.getText().toString());
}else {
Toast.makeText(this, "验证码不能为空", 1).show();
}
break;
case R.id.btn_supporst_country://国家列表
SMSSDK.getSupportedCountries();
//SMSSDK.getCountry(arg0);
SMSSDK.getGroupedCountryList();
//SMSSDK.getCountryByMCC(arg0);
break;
default:
break;
}
}
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
int event = msg.arg1;
int result = msg.arg2;
Object data = msg.obj;
Log.e("event", "event="+event);
if (result == SMSSDK.RESULT_COMPLETE) {
//短信注册成功后,返回MainActivity,然后提示新好友
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {//提交验证码成功
Toast.makeText(getApplicationContext(), "提交验证码成功", Toast.LENGTH_SHORT).show();
textView2.setText("提交验证码成功");
} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
Toast.makeText(getApplicationContext(), "验证码已经发送", Toast.LENGTH_SHORT).show();
textView2.setText("验证码已经发送");
}else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){//返回支持发送验证码的国家列表
Toast.makeText(getApplicationContext(), "获取国家列表成功", Toast.LENGTH_SHORT).show();
countryTextView.setText(data.toString());
}
} else {
((Throwable) data).printStackTrace();
int resId = getStringRes(MainActivity.this, "smssdk_network_error");
Toast.makeText(MainActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
if (resId > 0) {
Toast.makeText(MainActivity.this, resId, Toast.LENGTH_SHORT).show();
}
}
}
};
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
SMSSDK.unregisterAllEventHandler();
}
}
官方demo下载