膜拜大神之—-实现注册完整流程
package com.zsz.develop.registerutils;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
/**
* @author LigthWang
*
* 描述:注册用的封装
*/
public class SmsUitl {
public static final int TYPE_REGISTER = 0;
public static final int TYPE_LOGIN = 1;
/**
* 初始化获取验证码
*
* @param context
* @param etPhoneNum
* 手机号输入框
* @param btnAcquire
* 获取验证码按钮
* @param type
* 类型
*/
public static void initAcquireCheckNum(final Context context,
final EditText etPhoneNum, final Button btnAcquire, final int type,
final String url) {
btnAcquire.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String strPhoneNum = etPhoneNum.getText().toString().trim();
if (strPhoneNum.length() == 0) {
ToastUtil.showToast(context, "请输入手机号码!");
return;
}
if (!(YanZhengUtil.isPhoneNumber(strPhoneNum))) {
ToastUtil.showToast(context, "请输入有效的手机号!");
return;
}
if (!(new CheckNetwork(context).CheckNetworkAvailable())) {
return;
}
checkPhoneNum(context, strPhoneNum, btnAcquire, type, url);
}
});
}
/**
* 检查当前账号是否被注册
*
* @param context
* @param phoneNum
* @param btnAcquire
* @param type
*/
private static void checkPhoneNum(final Context context,
final String phoneNum, final Button btnAcquire, final int type,
final String url) {
AjaxParams params = new AjaxParams();
params.put("user.username", phoneNum);
FinalHttp fh = new FinalHttp();
fh.post(url, params, new AjaxCallBack<Object>() {
@Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
super.onFailure(t, errorNo, strMsg);
}
@Override
public void onSuccess(Object t) {
super.onSuccess(t);
String result = t.toString();
System.out.println("t ======= " + t.toString());
if (type == TYPE_LOGIN) {
if (result.equals("ok")) {
} else if (result.equals("unregis")) {
} else if (result.equals("WaitingForCheck")) {
} else {
ToastUtil.showToast(context, "获取验证码失败");
}
} else if (type == TYPE_REGISTER) {
if (result.equals("ok")) {
} else if (result.equals("unregis")) {
startButtonTimer(btnAcquire);
acquireCheckNum(phoneNum, type, url);
} else if (result.equals("WaitingForCheck")) {
} else {
ToastUtil.showToast(context, "获取验证码失败");
}
} else {
ToastUtil.showToast(context, "获取验证码失败");
}
}
});
}
/**
* 请求验证码
*
* @param phoneNum
* @param type
*/
private static void acquireCheckNum(String phoneNum, final int type,
String url) {
AjaxParams params = new AjaxParams();
params.put("user.phone", phoneNum);
params.put("type", type == TYPE_LOGIN ? "login" : "regeist");
FinalHttp fh = new FinalHttp();
fh.post(url, params, null);
}
/**
* 开始Button计时
*
* @param btnAcquire
*/
private static void startButtonTimer(final Button btnAcquire) {
btnAcquire.setEnabled(false);
btnAcquire.setTag(60);
btnAcquire.setText("已发送短信(60)");
btnAcquire.post(new ButtonTimerRunable(btnAcquire));
}
static class ButtonTimerRunable implements Runnable {
private Button btn;
public ButtonTimerRunable(Button btn) {
this.btn = btn;
}
@Override
public void run() {
try {
Integer leftSeconds = (Integer) btn.getTag();
if (leftSeconds <= 0) {
btn.setText("重新获取");
btn.setEnabled(true);
} else {
leftSeconds--;
btn.setText("已发送短信(" + leftSeconds + ")");
btn.setTag(leftSeconds);
btn.postDelayed(new ButtonTimerRunable(btn), 1000);
}
} catch (Exception e) {
btn.setText("重新获取");
btn.setEnabled(true);
}
}
}
}