自定义计数定时器

时间:2016-01-28 14:33:20

标签: android service timer chronometer

我的应用程序中需要一个倒计时器。我浏览了很多关于这个主题的论坛,但我找不到任何东西。实际上我明白我们可以用天文台做这个,但我有2个天文台问题

  1. 我不能在服务中使用天文台,因为天文台需要布局。
  2. 我无法将计时器初始化超过1小时。
  3. 我的代码在这里:

    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);
        }
    });
    

4 个答案:

答案 0 :(得分:8)

您可以反向使用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();

答案 1 :(得分:2)

每次值达到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();
        }
    });

答案 2 :(得分:2)

使用RxJava,您可以编写

Disposable var = Observable
            .interval(1, TimeUnit.SECONDS)
            .subscribe(
                    time -> {
                        long minutes = time / 60;
                        long second = time % 60;
                        timer.setText("" + minutes + ":" + second);
                    });

您可以使用以下方法停止计时器 var.dispose()

答案 3 :(得分:-1)

您可以使用此代码执行此操作: 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);
            }
        }
    };
}