注册或者登录获取验证码倒计时
/**
* 短信验证码的倒计时工具类
* Created kingsley on 2018/4/9 0009.
*/
public class CountDownMsgUtils {
private long TIMECODE_MAX;//只赋值一次
private String ACacheKey;
private Context context;
@ColorInt
private int defaultColor;
@ColorInt
private int changeColor;
/**
* 请求验证码的按钮
*/
private TextView requestBtn;
private ICountDownPostCode countDownPostCode;
@SuppressLint("HandlerLeak")
private Handler mcodeCountHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (requestBtn == null) {
return;
}
long nowTime = System.currentTimeMillis();
//ACache.get(context).getAsString(ACacheKey),这句代码获取保存的值也可以用下面代码代
//替SharedPreferences mPreference =PreferenceManager
//.getDefaultSharedPreferences(MainApplication.getContext());
//mPreference.getInt(ACacheKey,0);
long cacheTime = String2Long(ACache.get(context)
.getAsString(ACacheKey));
final long c = (nowTime - cacheTime) / 1000;
if (cacheTime == 0) {
return;
}
if (cacheTime != 0 && c > TIMECODE_MAX) {
ACache.get(context).put(ACacheKey, "0");
requestBtn.setTextColor(defaultColor);
requestBtn.setText("获取验证码");//获取验证码
} else {
mcodeCountHandler.sendMessageDelayed(new Message(), 1000);
requestBtn.setTextColor(changeColor);
requestBtn.setText((TIMECODE_MAX - c) + "s 后可重发");//剩余%1$s
}
}
};
public static class Builder {
private CountDownMsgUtils countDownMsgUtils;
public Builder(Context context) {
countDownMsgUtils = new CountDownMsgUtils();
countDownMsgUtils.context = context;
}
// public Builder setContext(Context context) {
// countDownMsgUtils.context = context;
// return this;
// }
public Builder setTime(long time) {
countDownMsgUtils.TIMECODE_MAX = time;
return this;
}
/**
* 设置默认眼色,即是点击获取验证码之前的颜色
*
* @param color
* @return
*/
public Builder setDefaultColor(String color) {
countDownMsgUtils.defaultColor = Color.parseColor(color);
return this;
}
/**
* 获取验证码之后的颜色
*
* @param color
* @return
*/
public Builder setChangeColor(String color) {
countDownMsgUtils.changeColor = Color.parseColor(color);
return this;
}
/**
* 获取验证码之后的颜色
*
* @param key
* @return
*/
public Builder setSaveTimeKey(String key) {
countDownMsgUtils.ACacheKey = key;
return this;
}
/**
* 获取显示倒计时的控件
*
* @return
*/
public Builder setBtnTextview(TextView textView) {
countDownMsgUtils.requestBtn = textView;
return this;
}
public CountDownMsgUtils build() {
countDownMsgUtils.mcodeCountHandler.sendMessage(countDownMsgUtils.mcodeCountHandler.obtainMessage());
return countDownMsgUtils;
}
}
public CountDownMsgUtils() {
}
/**
* @param ACacheKey,保存值的Key
* @param context 上下文
* @param TIMECODE_MAX,需要倒计时的时间
* @param requestBtn 要绑定控件即点击获取验证码的View
*/
public CountDownMsgUtils(String ACacheKey, Context context, long TIMECODE_MAX, TextView requestBtn) {
this.ACacheKey = ACacheKey;
this.context = context;
this.TIMECODE_MAX = TIMECODE_MAX;
this.requestBtn = requestBtn;
if (this.requestBtn == null) {
Toast.makeText(context, "", Toast.LENGTH_SHORT).show();
return;
}
mcodeCountHandler.sendMessage(mcodeCountHandler.obtainMessage());
}
/**
* 退出页面,回收资源
*/
public void clossHandler() {
if (requestBtn != null) {
requestBtn = null;
}
mcodeCountHandler.removeCallbacksAndMessages(null);
}
/**
* 放在请求验证码的点击事件中,经内部判断后才允许向服务器请求验证码
*
* @param countDownPostCode
*/
public void requestCheckCode(ICountDownPostCode countDownPostCode) {
if (this.requestBtn == null) {
return;
}
this.countDownPostCode = countDownPostCode;
long nowTime = System.currentTimeMillis();
long cacheTime = String2Long(ACache.get(context).getAsString(ACacheKey));
final long c = (nowTime - cacheTime) / 1000;
if (cacheTime == 0 || c > TIMECODE_MAX) {
countDownPostCode.allowToRequestCode();
} else {
Toast.makeText(context, "短信已发送,请稍后点击", Toast.LENGTH_SHORT).show();
}
}
/**
* String 转long
*
* @param o
* @return
*/
public final long String2Long(String o) {
if (o != null) {
try {
return Long.valueOf(o);
} catch (Exception e) {
return 0;
}
} else {
return 0;
}
}
/**
* 调用获取验证码接口成功,执行倒计时操作
*/
public void requestSuccess() {
//保存获取验证码成功后的时间,和当前时间对比,
ACache.get(context).put(ACacheKey, System.currentTimeMillis() + "");
mcodeCountHandler.sendMessage(new Message());
}
public interface ICountDownPostCode {
/**
* 允许请求验证码按钮点击
*/
void allowToRequestCode();
}
}
上面代码是工具类,
使用方法,
1,初始化工具类
countDownMsgUtils = new CountDownMsgUtils.Builder(this)
.setDefaultColor("#fc8952")
.setChangeColor("#CACACA")
.setBtnTextview(mBtnCode)//获取验证码的控件
.setTime(60).setSaveTimeKey("register_time").build();
2,开始获取验证码
在点击获取验证码控件的时候调用以下方法
countDownMsgUtils.requestCheckCode(new CountDownMsgUtils.ICountDownPostCode() {
@Override
public void allowToRequestCode() {
//调用接口获取验证码
getYourCode();
}
});
3,获取验证码成功,开始倒计时
这个代码是获取验证码吗成功的回调
countDownMsgUtils.requestSuccess();//获取短信成功开始倒计时
4,资源回收
@Override
public void onDestroy() {
super.onDestroy();
countDownMsgUtils.clossHandler();
}