public class NotpwdTelActivity extends Activity implements OnClickListener {
String APPKEY = "11bbdb5fe3eee";
String APPSECRETE = "ea0f2810f804d2d1086078e438754112";
// 手机号输入框
private EditText inputPhoneEt;
// 验证码输入框
private EditText inputCodeEt;
// 获取验证码按钮
private TextView requestCodeBtn;
// 注册按钮
private Button commitBtn;
//计时器
int i = 60;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_notpwdcode);
//初始化控件
init();
}
/**
* 初始化控件
*/
private void init() {
inputPhoneEt = (EditText) findViewById(R.id.edtTel);
inputCodeEt = (EditText) findViewById(R.id.edtCode);
requestCodeBtn = (TextView) findViewById(R.id.txtObtain);
commitBtn = (Button) findViewById(R.id.btnCode);
//点击事件声明
requestCodeBtn.setOnClickListener(this);
commitBtn.setOnClickListener(this);
// 启动短信验证sdk
SMSSDK.initSDK(this, APPKEY, APPSECRETE);
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 v) {
String phoneNums = inputPhoneEt.getText().toString();
switch (v.getId()) {
case R.id.txtObtain:
// 1. 通过规则判断手机号
if (!judgePhoneNums(phoneNums)) {
return;
} // 2. 通过sdk发送短信验证
SMSSDK.getVerificationCode("86", phoneNums);
// 3. 把按钮变成不可点击,并且显示倒计时(正在获取)
requestCodeBtn.setClickable(false);
requestCodeBtn.setText("重新发送(" i ")");
new Thread(new Runnable() {
@Override
public void run() {
for (; i > 0; i--) {
handler.sendEmptyMessage(-9);
if (i <= 0) {
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
handler.sendEmptyMessage(-8);
}
}).start();
break;
case R.id.btnCode:
//将收到的验证码和手机号提交再次核对
SMSSDK.submitVerificationCode("86", phoneNums, inputCodeEt
.getText().toString());
break;
}
}
/**
*获取验证码SDK
*/
@SuppressLint("HandlerLeak") Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == -9) {
requestCodeBtn.setText("重新发送(" i ")");
} else if (msg.what == -8) {
requestCodeBtn.setText("获取验证码");
requestCodeBtn.setClickable(true);
i = 30;
} else {
int event = msg.arg1;
int result = msg.arg2;
Object data = msg.obj;
Log.e("event", "event=" event " " result " " data);
if (result == SMSSDK.RESULT_COMPLETE) {
// 短信注册成功后,返回MainActivity,然后提示
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {// 提交验证码成功
Toast.makeText(getApplicationContext(), "提交验证码成功",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(NotpwdTelActivity.this,
NotpwdActivity.class);
intent.putExtra("telNo", inputPhoneEt.getText().toString());
startActivity(intent);
finish();
} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
Toast.makeText(getApplicationContext(), "正在获取验证码",
Toast.LENGTH_SHORT).show();
} else {
((Throwable) data).printStackTrace();
}
}
}
}
};
/**
* 判断手机号码是否合理
*
* @param phoneNums
*/
private boolean judgePhoneNums(String phoneNums) {
if (isMatchLength(phoneNums, 11)) {
return true;
}
Toast.makeText(this, "手机号码输入有误!",Toast.LENGTH_SHORT).show();
return false;
}
/**
* 判断一个字符串的位数
* @param str
* @param length
* @return
*/
@TargetApi(Build.VERSION_CODES.GINGERBREAD) public static boolean isMatchLength(String str, int length) {
if (str.isEmpty()) {
return false;
} else {
return str.length() ==length ? true : false;
}
}
/**
* progressbar
*/
@SuppressWarnings("unused")
private void createProgressBar() {
FrameLayout layout = (FrameLayout) findViewById(android.R.id.content);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
ProgressBar mProBar = new ProgressBar(this);
mProBar.setLayoutParams(layoutParams);
mProBar.setVisibility(View.VISIBLE);
layout.addView(mProBar);
}
@Override
protected void onDestroy() {
SMSSDK.unregisterAllEventHandler();
super.onDestroy();
}
}
评论