工具类
package com.sctek;
import java.lang.reflect.Field;
import android.os.CountDownTimer;
import android.util.Log;
/**
* 自定义倒计时类
* 当时间剩下2分钟时,开始红白交替闪动,当时间只剩下1分钟时加快闪动频率
*
* @author YeXiubiao 2012-01-02
*/
public abstract class CustomCountDownTimer extends CountDownTimer {
private static final long TIME_CHANGE_SPEED = 3 * 60 * 1000L; // 1分钟
private static final long TIME_FLASH = 3 * 60 * 1000L; // 2分钟
private static final long CALLBACK_SPEED = 500L; // 回调速度(0.5秒回调一次)
private boolean changeColor = false; // 颜色交替闪动变换标记
private boolean hasChangedColor = false; // 改变回调颜色的flag,控制只执行一次
private boolean hasChangedSpeed = false; // 改变回调速度的flag,控制只执行一次
public CustomCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onTick(final long millisUntilFinished) { // 重点是 此方法必须执行 可以是自己调用或者是系统调用 如同线程中的Run方法
Log.e("onTick", "........" + millisUntilFinished);
final long m = millisUntilFinished / 60000L; // 分;
// 格式化分钟的格式
final String minute;
if (m < 10) {
minute = "0" + m;
} else {
minute = String.valueOf(m);
}
// 格式化秒钟的格式
final long ss = (millisUntilFinished / 1000L) % 60L; // 秒
final String second;
if (ss < 10) {
second = "0" + ss;
} else {
second = String.valueOf(ss);
}
// 最终格式: 01:05
final String time = minute + ":" + second;
// 如果剩余时间小于TIME_FLASH则开始闪动(红白颜色交替变化)
if (millisUntilFinished <= TIME_FLASH && !hasChangedColor) {
changeColor = true;
//hasChangedColor = true; // 控制只执行一次
}
// 如果剩余时间小于2分钟则闪动频率加快(每0.5秒回调一次)
if (!hasChangedSpeed && millisUntilFinished <= TIME_CHANGE_SPEED) {
changeCountdownInterval(false);
//hasChangedSpeed = true; // 控制只执行一次
}
onTickCallBackTo(time, minute, second, changeColor);
}
//该方法是暴露给 业务层的方法 在 activity 及其相关的业务层调用 在该示例中发动该函数的是onTick方法
public abstract void onTickCallBackTo(final String value, final String min,
final String sec, boolean changeColor);
int count = 1;
long tims = 1000;
long result = 1000;
@SuppressWarnings("rawtypes")
/**改变回调频率*/
private void changeCountdownInterval(boolean flag) {
if (flag) {
count = 1;
}
if (count <= 5) {
result = tims * (2 * count - 1);
} else {
result = tims * 10;
}
count++;
try {
// 反射父类CountDownTimer的mCountdownInterval字段,动态改变回调频率
Class clazz = Class.forName("android.os.CountDownTimer");
Field field = clazz.getDeclaredField("mCountdownInterval");
field.setAccessible(true);
field.set(this, result);
Log.e("timer", "........" + result + "........." + count);
} catch (Exception e) {
Log.e("Ye", "反射类android.os.CountDownTimer失败: " + e);
}
}
public void controlTime(boolean flag) {
changeCountdownInterval(true);
}
}
activity 实现层
package com.sctek;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
//import cn.swiftpass.spay.R;
/**
* 自定义倒计时测试
*
* @author YeXiubiao 2012-01-02
*/
public class MainActivity extends Activity {
private TextView tv_timer;
private TextView tv_content;
private static final long MILLIS_IN_FUTURE = 4 * 60 * 1000; // 2分钟
private static final long COUNT_DOWN_INTERVAL = 1000; // 1秒
private boolean colorFlag = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_timer = (TextView) findViewById(R.id.tv_timer);
tv_content = (TextView) findViewById(R.id.tv_content);
startTimer();
}
private void startTimer() {
CustomCountDownTimer timer = new CustomCountDownTimer(MILLIS_IN_FUTURE, COUNT_DOWN_INTERVAL) {
@Override
public void onTickCallBackTo(String value, String min, String sec, boolean changeColor) {
if (changeColor) {
//控制颜色红白交替变化
if (colorFlag = !colorFlag) {
tv_timer.setTextColor(Color.RED);
} else {
tv_timer.setTextColor(Color.WHITE);
}
}
tv_timer.setText(value);
}
@Override
public void onFinish() {
tv_timer.setText("00:00");
tv_timer.setTextColor(Color.RED);
tv_content.setText("时间到了!");
tv_content.setTextColor(Color.RED);
controlTime(true);
}
};
timer.start();
}
}