自定义计数计时器
Custom Count Up Timer
我的应用程序中需要一个递增计时器。我浏览了很多关于这个主题的论坛,但我找不到任何东西。其实我知道我们可以用计时表来做到这一点,但是我有 2 个问题与计时表:
- 我无法在服务中使用计时表,因为计时表需要布局。
- 我无法将计时器初始化为超过 1 小时。
我的代码在这里:
stopWatch = new Chronometer (MainActivity.this);
startTime = SystemClock.elapsedRealtime();
stopWatch.start();
stopWatch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer arg0) {
countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
String asText = (countUp / 60) + ":" + (countUp % 60);
Log.i("t", asText);
}
});
您可以使用此代码来执行此操作:
https://gist.github.com/MiguelLavigne/8809180c5b8fe2fc7403
/**
* Simple timer class which count up until stopped.
* Inspired by {@link android.os.CountDownTimer}
*/
public abstract class CountUpTimer {
private final long interval;
private long base;
public CountUpTimer(long interval) {
this.interval = interval;
}
public void start() {
base = SystemClock.elapsedRealtime();
handler.sendMessage(handler.obtainMessage(MSG));
}
public void stop() {
handler.removeMessages(MSG);
}
public void reset() {
synchronized (this) {
base = SystemClock.elapsedRealtime();
}
}
abstract public void onTick(long elapsedTime);
private static final int MSG = 1;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
synchronized (CountUpTimer.this) {
long elapsedTime = SystemClock.elapsedRealtime() - base;
onTick(elapsedTime);
sendMessageDelayed(obtainMessage(MSG), interval);
}
}
};
}
您可以反向使用 countDownTimer 并获取经过的时间。
long totalSeconds = 30;
long intervalSeconds = 1;
CountDownTimer timer = new CountDownTimer(totalSeconds * 1000, intervalSeconds * 1000) {
public void onTick(long millisUntilFinished) {
Log.d("seconds elapsed: " , (totalSeconds * 1000 - millisUntilFinished) / 1000);
}
public void onFinish() {
Log.d( "done!", "Time's up!");
}
};
开始计时。
timer.start();
停止计时器。
timer.cancel();
每当值分别达到 59、59、23 时,秒、分钟和小时都会增加。每个值都显示在创建数字秒表的不同视图中。
checkin.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(final View view) {
checkin.setEnabled(false);
new CountDownTimer(300000000, 1000){
public void onTick(long millisUntilFinished){
sec++;
if(sec==59) {
min++;
sec=0;
}
if(min==59){
min=0;
hr++;
}
if(hr==23){
hr=00;
}
secView.setText(String.valueOf(sec));
minView.setText(String.valueOf(min));
hrView.setText(String.valueOf(hr));
}
public void onFinish(){
Snackbar.make(view, "Finish", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}.start();
}
});
使用RxJava,你可以写
Disposable var = Observable
.interval(1, TimeUnit.SECONDS)
.subscribe(
time -> {
long minutes = time / 60;
long second = time % 60;
timer.setText("" + minutes + ":" + second);
});
您可以使用
var.dispose()
我的应用程序中需要一个递增计时器。我浏览了很多关于这个主题的论坛,但我找不到任何东西。其实我知道我们可以用计时表来做到这一点,但是我有 2 个问题与计时表:
- 我无法在服务中使用计时表,因为计时表需要布局。
- 我无法将计时器初始化为超过 1 小时。
我的代码在这里:
stopWatch = new Chronometer (MainActivity.this);
startTime = SystemClock.elapsedRealtime();
stopWatch.start();
stopWatch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer arg0) {
countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
String asText = (countUp / 60) + ":" + (countUp % 60);
Log.i("t", asText);
}
});
您可以使用此代码来执行此操作: https://gist.github.com/MiguelLavigne/8809180c5b8fe2fc7403
/**
* Simple timer class which count up until stopped.
* Inspired by {@link android.os.CountDownTimer}
*/
public abstract class CountUpTimer {
private final long interval;
private long base;
public CountUpTimer(long interval) {
this.interval = interval;
}
public void start() {
base = SystemClock.elapsedRealtime();
handler.sendMessage(handler.obtainMessage(MSG));
}
public void stop() {
handler.removeMessages(MSG);
}
public void reset() {
synchronized (this) {
base = SystemClock.elapsedRealtime();
}
}
abstract public void onTick(long elapsedTime);
private static final int MSG = 1;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
synchronized (CountUpTimer.this) {
long elapsedTime = SystemClock.elapsedRealtime() - base;
onTick(elapsedTime);
sendMessageDelayed(obtainMessage(MSG), interval);
}
}
};
}
您可以反向使用 countDownTimer 并获取经过的时间。
long totalSeconds = 30;
long intervalSeconds = 1;
CountDownTimer timer = new CountDownTimer(totalSeconds * 1000, intervalSeconds * 1000) {
public void onTick(long millisUntilFinished) {
Log.d("seconds elapsed: " , (totalSeconds * 1000 - millisUntilFinished) / 1000);
}
public void onFinish() {
Log.d( "done!", "Time's up!");
}
};
开始计时。
timer.start();
停止计时器。
timer.cancel();
每当值分别达到 59、59、23 时,秒、分钟和小时都会增加。每个值都显示在创建数字秒表的不同视图中。
checkin.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(final View view) {
checkin.setEnabled(false);
new CountDownTimer(300000000, 1000){
public void onTick(long millisUntilFinished){
sec++;
if(sec==59) {
min++;
sec=0;
}
if(min==59){
min=0;
hr++;
}
if(hr==23){
hr=00;
}
secView.setText(String.valueOf(sec));
minView.setText(String.valueOf(min));
hrView.setText(String.valueOf(hr));
}
public void onFinish(){
Snackbar.make(view, "Finish", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}.start();
}
});
使用RxJava,你可以写
Disposable var = Observable
.interval(1, TimeUnit.SECONDS)
.subscribe(
time -> {
long minutes = time / 60;
long second = time % 60;
timer.setText("" + minutes + ":" + second);
});
您可以使用
var.dispose()