新人一个,请大家多多指教!
1.自己去Mob官网下载一个短信的sdk
2.解压文件打开,有一个SMSSDK的文件夹,打开后有五个文件,只复制一下这四个就行(MobCommons-2017.0321.1624.jar,MobTools-2017.0321.1624.jar,
SMSSDK-2.1.4.aar,SMSSDKGUI-2.1.4.aar),(studio)粘贴到自己创建的项目下的libs目录下。
3.将这句代粘贴到build.gridle文件下,自己单独一个位置
repositories{
flatDir{
dirs 'libs' //就是你放aar的目录地址
}
}
然后再将这两句粘贴build.gridle文件的dependencies下
compile name:'SMSSDK-2.1.4',ext:'aar'
compile name:'SMSSDKGUI-2.1.4',ext:'aar'
4.清单文件中添加权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5.mainactivity界面布局文件
<EditText
android:hint="输入手机号"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_phone"/>
<EditText
android:hint="输入验证码"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_code"/>
<Button
android:text="接收验证码"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt_getCode"/>
<Button
android:text="发送验证码"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt_verify"/>
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import static cn.smssdk.SMSSDK.getSupportedCountries;
import static cn.smssdk.SMSSDK.getVerificationCode;
import static cn.smssdk.SMSSDK.submitVerificationCode;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btnrequest,btnsub;
private EditText etphone,etcode;
int i =60;
Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == -9) {
btnrequest.setText("重新发送(" + i + ")");
} else if (msg.what == -8) {
btnrequest.setText("获取验证码");
btnrequest.setClickable(true); // 设置可点击
i = 30;
} else {
int event = msg.arg1;
int result = msg.arg2;
Object data = msg.obj;
if (result == SMSSDK.RESULT_COMPLETE) {
// 短信注册成功后,返回MainActivity,然后提示
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {// 提交验证码成功
Toast.makeText(getApplicationContext(), "提交验证码成功",
Toast.LENGTH_SHORT).show();
// 验证成功后跳转主界面
//此处可注释掉
Intent intent = new Intent(MainActivity.this, reg.class);
intent.putExtra("flag", "newuser");
startActivity(intent);
Log.e("注册", "成功!");
finish();// 成功跳转之后销毁当前页面
} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
Toast.makeText(getApplicationContext(), "验证码已经发送",
Toast.LENGTH_SHORT).show();
} else {
((Throwable) data).printStackTrace();
}
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SMSSDK.initSDK(this, "1df6e8fd96cec", "674e88e1e2a68af8a5cbe1551dcb9769");
etphone = (EditText) findViewById(R.id.et_phone);
etcode = (EditText) findViewById(R.id.et_code);
btnrequest = (Button) findViewById(R.id.bt_getCode);
btnsub = (Button) findViewById(R.id.bt_verify);
btnrequest.setOnClickListener(this);
btnsub.setOnClickListener(this);
Log.e("ms", "1");
// 启动短信验证sdk
SMSSDK.initSDK(this, "186af2e954049",
"48466e82249da303af399acb41f5e703");
EventHandler eventHandler = 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(eventHandler); // 注册回调监听接口
};
@Override
public void onClick(View view) {
String phoneNums = etphone.getText().toString();
switch (view.getId()) {
case R.id.bt_getCode:
if (!judgePhoneNums(phoneNums)) {// 判断输入号码是否正确
return;
}
SMSSDK.getVerificationCode("86", phoneNums); // 调用sdk发送短信验证
btnrequest.setClickable(false);// 设置按钮不可点击 显示倒计时
btnrequest.setText("重新发送(" + i + ")");
new Thread(new Runnable() {
@Override
public void run() {
for (i = 30; i > 0; i--) {
handler.sendEmptyMessage(-9);
if (i <= 0) {
break;
}
try {
Thread.sleep(1000);// 线程休眠实现读秒功能
} catch (InterruptedException e) {
e.printStackTrace();
}
}
handler.sendEmptyMessage(-8);// 在30秒后重新显示为获取验证码
}
}).start();
break;
case R.id.bt_verify:
SMSSDK.submitVerificationCode("86", phoneNums, etcode.getText()
.toString());
// createProgressBar();
break;
}
}
/**
* 判断手机号码是否合理
*
* @param phoneNums
*/
private boolean judgePhoneNums(String phoneNums) {
if (isMatchLength(phoneNums, 11) && isMobileNO(phoneNums)) {
return true;
}
Toast.makeText(this, "手机号码输入有误!", Toast.LENGTH_SHORT).show();
return false;
}
public static boolean isMatchLength(String str, int length) {
if (str.isEmpty()) {
return false;
} else {
return str.length() == length ? true : false;
}
}
public static boolean isMobileNO(String mobileNums) {
String telRegex = "[1][3578]\\d{9}";// "[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。
if (TextUtils.isEmpty(mobileNums))
return false;
else
return mobileNums.matches(telRegex);
}
@Override
protected void onDestroy() {
SMSSDK.unregisterAllEventHandler();
super.onDestroy();
}
}
7.最后别忘了创建一个activity界面,用于跳转,配置一下