一、配置
在根目录的build.gradle添加:
buildscript{
repositories{
maven{url"http://mvn.mob.com/android"} //mob
}
dependencies{
classpath'com.mob.sdk:MobSDK:+' //mob
}
}
在当前工程的build.gradle添加:
applyplugin:'com.mob.sdk' //mob
MobSDK{
appKey"3215c1e94b289"
appSecret"88b943e3be4437d1f78ea9c416c53620"
SMSSDK{
mobileAuthtrue
autoSMStrue
}
}
在manifest中添加:
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" /> <!--mob-->
并在application里面添加:
<activity android:name=".forgetpwdphone"
android:configChanges="keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="stateHidden|adjustResize"/>
xml文件:
参考:https://blog.csdn.net/program_dancing/article/details/102771519
drawable里面的这些自己根据喜好添加
java文件:
package com.example.smartcommunityv2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AlertDialog;
import android.os.Bundle;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.mob.MobSDK; //mob
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK; //mob
public class forgetpwdphoneextends AppCompatActivity {
private TimerTasktt; //mob
private Timertm;
private EditTextet_phonenum;
private Buttonbtn_check;
private EditTextet_checkecode;
private Buttonbtn_sure;
private int TIME =60;//倒计时60s这里应该多设置些因为mob后台需要60s,我们前端会有差异的建议设置90,100或者120
public Stringcountry="86";//这是中国区号,如果需要其他国家列表,可以使用getSupportedCountries();获得国家区号
private Stringphone;
private static final int CODE_REPEAT =1; //重新发送//mob
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgetpwdphone);
MobSDK.init(this, "3215c1e94b289", "88b943e3be4437d1f78ea9c416c53620"); //mob
SMSSDK.registerEventHandler(eh); //注册短信回调(记得销毁,避免泄露内存)
initView(); //mob
}
Handlerhd =new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what ==CODE_REPEAT) {
btn_check.setEnabled(true);
btn_sure.setEnabled(true);
tm.cancel();//取消任务
tt.cancel();//取消任务
TIME =60;//时间重置
btn_check.setText("重新发送验证码");
}else {
btn_check.setText(TIME +"重新发送验证码");
}
}
};
//回调
EventHandlereh=new EventHandler(){
public void afterEvent(int event, int result, Object data) {
if (result == SMSSDK.RESULT_COMPLETE) {
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
toast("验证成功");
Intent intent =new Intent(forgetpwdphone.this, login.class);
startActivity(intent);
}else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){//获取验证码成功
toast("获取验证码成功");
}else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){//如果你调用了获取国家区号类表会在这里回调
//返回支持发送验证码的国家列表
}
}else{//错误等在这里(包括验证失败)
//错误码请参照http://wiki.mob.com/android-api-错误码参考/这里我就不再继续写了
((Throwable)data).printStackTrace();
String str = data.toString();
toast(str);
}
}
};
private void toast(final String str) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(forgetpwdphone.this, str, Toast.LENGTH_SHORT).show();
}
});
}
private void initView() {
et_phonenum = (EditText) findViewById(R.id.et_phonenum);
btn_check = (Button) findViewById(R.id.btn_check);
et_checkecode = (EditText) findViewById(R.id.et_checkecode);
btn_sure = (Button) findViewById(R.id.btn_sure);
btn_check.setOnClickListener(this::onClick);
btn_sure.setOnClickListener(this::onClick);
}
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btn_check:
phone =et_phonenum.getText().toString().trim().replaceAll("/s","");
if (!TextUtils.isEmpty(phone))
{
//定义需要匹配的正则表达式的规则
String REGEX_MOBILE_SIMPLE ="^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
//把正则表达式的规则编译成模板
Pattern pattern = Pattern.compile(REGEX_MOBILE_SIMPLE);
//把需要匹配的字符给模板匹配,获得匹配器
Matcher matcher = pattern.matcher(phone);
// 通过匹配器查找是否有该字符,不可重复调用重复调用matcher.find()
if (matcher.find())
{//匹配手机号是否存在
alterWarning();
}
else
{
toast("手机号格式错误");
}
}
else
{
toast("请先输入手机号");
}
break;
case R.id.btn_sure:
//获得用户输入的验证码
String code =et_checkecode.getText().toString().replaceAll("/s","");
if (!TextUtils.isEmpty(code))
{//判断验证码是否为空
//验证
SMSSDK.submitVerificationCode(country, phone, code);
}
else if(phone.length()==0&code.length()!=0)
{
toast("请输入手机号");
}
else if(phone.length()==0&code.length()==0)
{
toast("请输入完整信息");
}
break;
}
}
//弹窗确认下发
private void alterWarning() {
//构造器
AlertDialog.Builder builder =new AlertDialog.Builder(this);
builder.setTitle("星际通告"); //设置标题
builder.setMessage("我们将要发送伽马信号到坐标为" +phone +"的星球进行验证"); //设置内容
builder.setIcon(R.mipmap.spongebob);//设置图标,图片id即可
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//设置确定按钮
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); //关闭dialog
//通过sdk发送短信验证(请求获取短信验证码,在监听(eh)中返回)
SMSSDK.getVerificationCode(country, phone);
//做倒计时操作
Toast.makeText(forgetpwdphone.this, "已发送" + which, Toast.LENGTH_SHORT).show();
btn_check.setEnabled(false);
btn_sure.setEnabled(true);
tm =new Timer();
tt =new TimerTask() {
@Override
public void run() {
hd.sendEmptyMessage(TIME--);
}
};
tm.schedule(tt,0,1000);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {//设置取消按钮
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(forgetpwdphone.this, "已取消" + which, Toast.LENGTH_SHORT).show();
}
});
//参数都设置完成了,创建并显示出来
builder.create().show();
}
//销毁短信注册
@Override
protected void onDestroy() {
super.onDestroy();
// 注销回调接口registerEventHandler必须和unregisterEventHandler配套使用,否则可能造成内存泄漏。
SMSSDK.unregisterEventHandler(eh);
}
}