常用计时器实现

时间:2022-05-09 20:34:06

项目开发中常常会用到计时器

例如,获取验证码的时候,商城类应用的限时抢购,考试类应用的考试计时

两种实现方式:

1,定义一个计时类

提供以下方法:

开始倒计时–startTime()

刷新计时–refreshTime(long second)

停止计时–stopTime()

获取现在时间–getNowTime()

获取message.what的TAG –getTAG()

  /**
* 计时器不使用内部类的时候一定要注意生命周期管理
* 传入的时间用秒做单位
* Created by Administrator on 2017/3/29.
*/


public class ClockUtils extends Activity {
private long time ;
private Handler mHandler;
private TextView mView;
private int REFRESH_LIMIT = 2017;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

public ClockUtils(long second, Handler handler, TextView view) {
this.time = second;
this.mHandler = handler;
this.mView = view;
}

public void startTime(){
runnable.run();
}

public void refreshTime(long second){
this.time = second;
runnable.run();
}

public long getNowTime(){
return time<0?0:time;
}

public void stopTime(){
timeHandler.removeCallbacks(runnable);
}

public int getTAG(){
return REFRESH_LIMIT;
}

Handler timeHandler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
if (time>0){
time--;
timeHandler.postDelayed(this, 1000);
//需要显示小时的倒计时
// long hour = (time / (60 * 60));
// long minute = ((time / 60 ) - hour * 60);
// long second = (time - (hour * 60 * 60 )- (minute * 60));
// mView.setText(timeFormatting(hour) + ":" + timeFormatting(minute) + ":" + timeFormatting(second));
//只需要从分钟开始显示的倒计时
long minute = ((time / 60 ));
long second = (time - (minute * 60));
mView.setText(timeFormatting(minute) + ":" + timeFormatting(second));
}else
if (time == 0){
//时间走完,告知activity处理
Message message = new Message();
message.what = REFRESH_LIMIT;
mHandler.sendMessage(message);
}
}
};

@Override
protected void onDestroy() {
super.onDestroy();
timeHandler.removeCallbacks(runnable);
}

// 时间格式化,在小于10的情况下前面自动补0
private String timeFormatting(long number) {
String str = String.valueOf(number);
if (str.length() < 2) {
str = "0" + str;
}
return str;
}
}

特别重要的一点,不可见的时候要停止计时

2,使用handler-message

以获取验证码的倒计时为例

 private void getVerifyCode() {
phone = etPhoneVerify.getText().toString();
boolean b = CommUtils.isPhoneValid(phone);
if (b) {
if (recLen == 60){
getVerify(phone);
showTime();
}else {
ToastUtil.showShort(RegisterActivity.this,"正在请求,请稍后", Gravity.CENTER);
}
} else {
ToastUtil.showShort(this,"请检查手机号是否有误", Gravity.CENTER);
}
}

//显示验证码有效时间倒计时
private void showTime() {
handler.postDelayed(runnable, 1000);
}

private int recLen = 60;//设置有效时间
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
recLen--;
tvGetVerify.setText("有效时间:" + recLen);
handler.postDelayed(this, 1000);
if (recLen <= 0) {
recLen = 60;//重置
Message message = new Message();
message.what = 1;
handlerStop.sendMessage(message);
tvGetVerify.setText("获取验证码");
}
}
};

final Handler handlerStop = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
handler.removeCallbacks(runnable);
break;
}
super.handleMessage(msg);
}

};

@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
}