MOB 短信验证
工具/原料
-
Android Studio
-
mob SDK中的jar 和.so文件
方法/步骤
-
把3个jar 放入libs
-
并添加依赖 在项目的build.gradle里面
-
在你的项目的main 目录下新建jniLibs 文件夹 注意:!!!! 一定是main 目录下 不是 java 文件夹
-
写代码!一步一步复制吧 SMSSDK.initSDK(this, " ", " ");
-
EventHandler eh = new EventHandler() { @Override public void afterEvent(int event, int result, Object data) { LogUtils.i("event:"+event+" result:"+result+" data:"+data.toString()); switch (event) { case SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE: if (result == SMSSDK.RESULT_COMPLETE) { toast("验证成功"); } else { toast("验证失败"); } break; case SMSSDK.EVENT_GET_VERIFICATION_CODE: if (result == SMSSDK.RESULT_COMPLETE) { toast("获取验证码成功"); //默认的智能验证是开启的,我已经在后台关闭 } else { toast("获取验证码失败"); } break; } }};
-
这里是获得验证码的请求: SMSSDK.getVerificationCode("86", phone, new OnSendMessageHandler() { @Override public boolean onSendMessage(String s, String s1) { return false; }});
-
这里是提交用户输入的验证码:SMSSDK.submitVerificationCode("86", phone, SMS);
-
对了 还有toast
private void toast(final String str) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(RegisterActivity.this, str, Toast.LENGTH_SHORT).show();
}
});
}
-
完整 demo
package com.vipheyue.swust.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.dd.processbutton.iml.SubmitProcessButton;
import com.lidroid.xutils.util.LogUtils;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.vipheyue.swust.R;
import com.vipheyue.swust.domain.MyUser;
import com.vipheyue.swust.utils.ProgressGenerator;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.listener.SaveListener;
import cn.smssdk.EventHandler;
import cn.smssdk.OnSendMessageHandler;
import cn.smssdk.SMSSDK;
public class RegisterActivity extends Activity implements ProgressGenerator.OnCompleteListener {
EventHandler eh = new EventHandler() {
@Override
public void afterEvent(int event, int result, Object data) {
LogUtils.i("event:"+event+" result:"+result+" data:"+data.toString());
switch (event) {
case SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE:
if (result == SMSSDK.RESULT_COMPLETE) {
toast("验证成功");
} else {
toast("验证失败");
}
break;
case SMSSDK.EVENT_GET_VERIFICATION_CODE:
if (result == SMSSDK.RESULT_COMPLETE) {
toast("获取验证码成功");
//默认的智能验证是开启的,我已经在后台关闭
} else {
toast("获取验证码失败");
}
break;
}
}
};
private void toast(final String str) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(RegisterActivity.this, str, Toast.LENGTH_SHORT).show();
}
});
}
@Bind(R.id.met_phone)
MaterialEditText metPhone;
@Bind(R.id.btn_Send)
SubmitProcessButton btnSend;
@Bind(R.id.et_codeSMS)
EditText etCodeSMS;
@Bind(R.id.bt_commit)
Button btCommit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
ButterKnife.bind(this);
LogUtils.customTagPrefix = "xUtils"; LogUtils.allowI = true;
SMSSDK.initSDK(this, "bed3c2b8490b", "aa7933d433ce4a6b7b1d895f93312fef");
SMSSDK.registerEventHandler(eh); //注册短信回调
getSMS();
}
private void getSMS() {
String phone = metPhone.getText().toString();
SMSSDK.getVerificationCode("86", phone, new OnSendMessageHandler() {
@Override
public boolean onSendMessage(String s, String s1) {
return false;
}
});
}
@OnClick(R.id.bt_commit)
public void onClick() {
String SMS=etCodeSMS.getText().toString().trim();
String phone = metPhone.getText().toString();
SMSSDK.submitVerificationCode("86", phone, SMS);
}
}
-
注意到mob后台去把只能短信 关闭哟