一.导依赖
1.在项目的build.gradle
classpath 'com.mob.sdk:MobSDK:+'
2.在app的build.gradle
// 添加插件
apply plugin: 'com.mob.sdk'
// 在MobSDK的扩展中注册SMSSDK的相关信息
MobSDK {
appKey "d580ad56b4b5"
appSecret "7fcae59a62342e7e2759e9e397c82bdd"
SMSSDK {}
}
二.配置全局App
MobSDK.init(this);
三.可以使用自带面板的和自定义面板
1.自带面板(什么都不用写,直接复制下面这段代码就可以了)
/**
* 带面板的(可用)
* 发送验证码
* @param context
*/
public void sendCode(Context context) {
RegisterPage page = new RegisterPage();
//如果使用我们的ui,没有申请模板编号的情况下需传null
page.setTempCode(null);
page.setRegisterCallback(new EventHandler() {
public void afterEvent(int event, int result, Object data) {
if (result == SMSSDK.RESULT_COMPLETE) {
// 处理成功的结果
HashMap<String,Object> phoneMap = (HashMap<String, Object>) data;
String country = (String) phoneMap.get("country"); // 国家代码,如“86”
String phone = (String) phoneMap.get("phone"); // 手机号码,如“13800138000”
// TODO 利用国家代码和手机号码进行后续的操作
} else{
// TODO 处理错误的结果
}
}
});
page.show(context);
}
2.自定义面板
布局
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/et_tel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="请输入手机号" />
<TextView
android:id="@+id/et_yanz"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="发送验证码"
android:textSize="25sp" />
</LinearLayout>
<EditText
android:id="@+id/et_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入验证码" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="verifyCode"
android:text="验证"
android:textSize="18sp" />
</LinearLayout>
java代码
package com.example.ggq.sendsms;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DialogTitle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashMap;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private EditText et_tel;
private EditText et_code;
private EventHandler eventHandler = new EventHandler() {
public void afterEvent(int event, int result, Object data) {
// afterEvent会在子线程被调用,因此如果后续有UI相关操作,需要将数据发送到UI线程
Message msg = new Message();
msg.arg1 = event;
msg.arg2 = result;
msg.obj = data;
new Handler(Looper.getMainLooper(), new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
int event = msg.arg1;
int result = msg.arg2;
Object data = msg.obj;
if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
if (result == SMSSDK.RESULT_COMPLETE) {
// TODO 处理成功得到验证码的结果
// 请注意,此时只是完成了发送验证码的请求,验证码短信还需要几秒钟之后才送达
Toast.makeText(MainActivity.this, "验证码发送成功,请注意接收", Toast.LENGTH_SHORT).show();
} else {
// TODO 处理错误的结果
((Throwable) data).printStackTrace();
Toast.makeText(MainActivity.this, "验证码发送失败,请输入正确的手机号!!!", Toast.LENGTH_SHORT).show();
}
} else if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
if (result == SMSSDK.RESULT_COMPLETE) {
// TODO 处理验证码验证通过的结果
Toast.makeText(MainActivity.this, "恭喜验证通过", Toast.LENGTH_SHORT).show();
} else {
// TODO 处理错误的结果
((Throwable) data).printStackTrace();
Toast.makeText(MainActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
}
}
// TODO 其他接口的返回结果也类似,根据event判断当前数据属于哪个接口
return false;
}
}).sendMessage(msg);
}
};
private String tel;
private TextView tv_sendMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//带面板的发送短信验证码并验证的方法(可用)
//sendCode(this);
//初始化控件
initView();
// 注册一个事件回调,用于处理SMSSDK接口请求的结果
SMSSDK.registerEventHandler(eventHandler);
//点击按钮发送验证码
tv_sendMsg.setOnClickListener(this);
}
private void initView() {
et_tel = findViewById(R.id.et_tel);
et_code = findViewById(R.id.et_code);
tv_sendMsg= findViewById(R.id.et_yanz);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 使用完EventHandler需注销,否则可能出现内存泄漏
SMSSDK.unregisterEventHandler(eventHandler);
}
/**
* 点击验证
* @param view
*/
public void verifyCode(View view) {
String code = et_code.getText().toString();
// 提交验证码,其中的code表示验证码,如“1357”
SMSSDK.submitVerificationCode("86", tel, code);
}
@Override
public void onClick(View v) {
tel = et_tel.getText().toString();
// 请求验证码,其中country表示国家代码,如“86”;phone表示手机号码,如“13800138000”
SMSSDK.getVerificationCode("86", tel);
}
}
//最后这个没有用,请随便看看
//在尝试读取通信录时以弹窗提示用户(可选功能)
SMSSDK.setAskPermisionOnReadContact(true);