去官网注册 MobSDK ,注册完成之后进入开发者平台创建应用
其中App Key和App Secret我们待会要用,然后这边就完事了
接着到我们应用中来
1、在目录app外的build里添加集成
即 在buildscript下repositories里添加
maven {
url "http://mvn.mob.com/android"
}
在dependencies里添加
classpath "com.android.tools.build:gradle:4.0.0"
classpath 'com.mob.sdk:MobSDK:+'
如下图红框 是咱们要添加的
2、在app内的build里面
添加
// 添加插件
apply plugin: 'com.mob.sdk'
// 在MobSDK的扩展中注册SMSSDK的相关信息
MobSDK {
appKey "m3005e***"//刚刚的appKey
appSecret "359494f89e8855e***"//刚刚的appSecret
SMSSDK {}
}
如下图
请求短信接口 https://webapi.sms.mob.com/sms/verify
需要传参
RequestBody requestBody = new FormBody.Builder()
.add(“appkey”, appkey)//你的appkey
.add(“zone”, zone)//区号 86
.add(“code”, codes)//接收的验证码
.add(“phone”, phone)//手机号
.build();
注册activity
public class RegisterActivity extends AppCompatActivity {
EditText username,email,code,password,rePassword;
private TextView sendCode;
private int times = 60;
public String appkey = "m3005e98e**";//你的appkey
public String zone = "86";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register_activity);
init();
}
private void init(){
sendCode = (TextView)findViewById(R.id.get_code);//发送验证码
username = (EditText)findViewById(R.id.edusername);
email = (EditText)findViewById(R.id.edemail);
code = (EditText)findViewById(R.id.edCode);
password = (EditText)findViewById(R.id.edpassword);
rePassword = (EditText)findViewById(R.id.edRepassword);
Log.i("tests","tests___________");
}
public void goBack(View v){//返回
finish();
}
public void getCode(View v) {//获取验证码
String phone = email.getText().toString();
countNum();
//在onCreate方法初始化SDK 短信注册
MobSDK.init(RegisterActivity.this,"你的appkey","你的appSecret");
SMSSDK.registerEventHandler(eh);
Log.i("test","testc"+phone);
SMSSDK.getVerificationCode("86",phone);
}
EventHandler eh=new EventHandler() {//短信回调
@Override
public void afterEvent(int event, int result, Object data) {
if (result == SMSSDK.RESULT_COMPLETE) {
//回调完成
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
//提交验证码成功
Log.i("EventHandler", "提交验证码成功");
}else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
//获取验证码成功
Log.i("EventHandler", "获取验证码成功");
}else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
//返回支持发送验证码的国家列表
Log.i("EventHandler", "返回支持发送验证码的国家列表");
}
}else{
((Throwable)data).printStackTrace();
Log.i("EventHandler", "回调失败");
}
}
};
private void countNum(){//发送验证码计时
CountDownTimer timer = new CountDownTimer(60000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
sendCode.setEnabled(false);
sendCode.setText("已发送(" + millisUntilFinished / 1000 + ")");
}
@Override
public void onFinish() {
sendCode.setEnabled(true);
sendCode.setText("重新获取");
}
}.start();
}
public void registerInfo(View v){//注册
String phone = email.getText().toString();
String codes = code.getText().toString();
RequestBody requestBody = new FormBody.Builder()
.add("appkey", appkey)
.add("zone", zone)//区号 86
.add("code", codes)//接收的验证码
.add("phone", phone)//手机号
.build();
HttpRegister.sendOkHttpRequestCheckCode("https://webapi.sms.mob.com/sms/verify",requestBody, new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
public void onResponse(Call call, Response response) throws IOException {
String resposeData = response.body().string();
Gson gson = new GsonBuilder()
.registerTypeAdapter(new TypeToken<Map<String, Long>>(){
}.getType(),new MyTypeAdapter()).create();
Map<String,Long> dataDetail = gson.fromJson(resposeData,new TypeToken<Map<String,Long>>(){
}.getType());//gson类型转换
Long codeId = dataDetail.get("status");
Map<String,String> users = new HashMap<>();
users.put("userName",username.getText().toString());
users.put("password",password.getText().toString());
users.put("phone",email.getText().toString());
if (codeId == 200) {
Log.d("msg","注册成功");
// 注册逻辑
register(users);
} else if (codeId == 405) {
Log.d("msg","AppKey为空");
} else if (codeId == 406) {
Log.d("msg","AppKey无效");
} else if (codeId == 456) {
Log.d("msg","手机号码为空");
} else if (codeId == 457) {
Log.d("msg","手机号码格式错误");
} else if (codeId == 466) {
Log.d("msg","请求验证的验证码为空");
} else if (codeId == 467) {
Log.d("msg","请求验证验证码重复");
} else if (codeId == 468) {
Log.d("msg","验证码错误");
} else if (codeId == 474) {
Log.d("msg","没有打开服务端验证开关");
} else {
throw new IllegalStateException("Unexpected value: " + codeId);
}
//
}
});
}
public void register(Map users) {//注册
}
}
layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg3"
>
<LinearLayout
android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:orientation="horizontal"
>
<ImageView
android:layout_marginLeft="@dimen/baseMargin"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="goBack"
android:src="@drawable/back"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/top_title2"
android:layout_marginRight="@dimen/baseMargin"
android:gravity="center"
android:textSize="16dp"
android:textColor="@color/white"/>
</LinearLayout>
<LinearLayout
android:layout_below="@+id/top"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/usernameLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-60dp"
android:layout_marginRight="@dimen/baseMargin"
android:layout_marginLeft="@dimen/baseMargin"
android:orientation="vertical"
android:layout_below="@+id/top">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_height="30dp">
<ImageView
android:id="@+id/textUsername"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/baseMargin"
android:src="@drawable/xingmingyonghumingnicheng"
/>
<EditText
android:id="@+id/edusername"
android:layout_toRightOf="@+id/textUsername"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="@dimen/baseSize"
android:background="@color/nonColor"
android:textColor="@color/likeWhite"
android:hint="@string/usernameHint"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:background="#ddd"/>
</LinearLayout>
<LinearLayout
android:id="@+id/emailLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginRight="@dimen/baseMargin"
android:layout_marginLeft="@dimen/baseMargin"
android:orientation="vertical"
android:layout_below="@+id/usernameLay">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_height="30dp">
<ImageView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/baseMargin"
android:src="@drawable/phone"
/>
<EditText
android:id="@+id/edemail"
android:layout_toRightOf="@+id/email"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="@dimen/baseSize"
android:background="@color/nonColor"
android:textColor="@color/likeWhite"
android:hint="@string/emailHint"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:background="#ddd"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginRight="@dimen/baseMargin"
android:layout_marginLeft="@dimen/baseMargin"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="30dp">
<ImageView
android:id="@+id/code"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/baseMargin"
android:src="@drawable/yanzhengma"/>
<EditText
android:id="@+id/edCode"
android:layout_toRightOf="@+id/code"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="@dimen/baseSize"
android:background="@color/nonColor"
android:textColor="@color/likeWhite"
android:hint="@string/code"/>
<TextView
android:id="@+id/get_code"
android:layout_alignParentRight="true"
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@color/white"
android:background="@color/baseColor"
android:textSize="@dimen/baseSize"
android:onClick="getCode"
android:text="@string/getcode"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:background="#ddd"/>
</LinearLayout>
<LinearLayout
android:id="@+id/passwordLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginRight="@dimen/baseMargin"
android:layout_marginLeft="@dimen/baseMargin"
android:orientation="vertical"
android:layout_below="@+id/emailLay">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="30dp">
<ImageView
android:id="@+id/textPassword"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/baseMargin"
android:src="@drawable/mima"/>
<EditText
android:id="@+id/edpassword"
android:layout_toRightOf="@+id/textPassword"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="@dimen/baseSize"
android:background="@color/nonColor"
android:textColor="@color/likeWhite"
android:hint="@string/passwordHint"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:background="#ddd"/>
</LinearLayout>
<LinearLayout
android:id="@+id/repasswordLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginRight="@dimen/baseMargin"
android:layout_marginLeft="@dimen/baseMargin"
android:orientation="vertical"
android:layout_below="@+id/passwordLay">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="30dp">
<ImageView
android:id="@+id/repassword"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/baseMargin"
android:src="@drawable/mima"/>
<EditText
android:id="@+id/edRepassword"
android:layout_toRightOf="@+id/repassword"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="@dimen/baseSize"
android:background="@color/nonColor"
android:textColor="@color/likeWhite"
android:hint="@string/rePassword"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:background="#ddd"/>
</LinearLayout>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="@dimen/baseMargin"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_weight="1"
android:gravity="right|center_vertical"
android:text="@string/top_title1"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/top_title2"
android:textColor="@color/white"
android:background="@color/baseColor"
android:onClick="registerInfo"
android:textSize="16dp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
其中参照很多大佬的成果,时隔有点久找不到链接了
源码:https://github.com/wuyunxiayaya/SendMessage