恢复&暂停基于处理程序的计时器

时间:2016-12-26 18:08:39

标签: java android android-studio

我已经设置了一个计时器,它显示了在应用程序屏幕上计算的时间。 当您点击开始按钮时,计时器正在运行,当您点击停止时,它会停止,但是当我尝试恢复它时,它会重新启动现有时间。

如何在不刷新/重新启动现有时间的情况下恢复计时器?

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;



public class Timer extends AppCompatActivity {

    TextView timerTextView;
    long startTime = 0;

    //runs without a timer by reposting this handler at the end of the runnable
    Handler timerHandler = new Handler();
    Runnable timerRunnable = new Runnable() {

        @Override
        public void run() {
            long millis = System.currentTimeMillis() - startTime;
            int seconds = (int) (millis / 1000);
            int minutes = seconds / 60;
            int hours = minutes / 60;
            seconds = seconds % 60;

            timerTextView.setText(String.format("%d:%02d:%02d", hours, minutes, seconds));

            timerHandler.postDelayed(this, 500);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer);

        timerTextView = (TextView) findViewById(R.id.timerTextView);

        Button b = (Button) findViewById(R.id.button);
        b.setText(R.string.start);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Button b = (Button) v;
                if (b.getText().equals("Stop")) {
                    timerHandler.removeCallbacks(timerRunnable);
                    b.setText(R.string.resume);
                } else {
                    startTime = System.currentTimeMillis();
                    timerHandler.postDelayed(timerRunnable, 0);
                    b.setText(R.string.stop);
                }
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        timerHandler.removeCallbacks(timerRunnable);
        Button b = (Button)findViewById(R.id.button);
        b.setText("start");
    }
}

1 个答案:

答案 0 :(得分:1)

试试这段代码:

long startTime = 0;
long elapsedTime ;

 b.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Button b = (Button) v;
        if (b.getText().equals("Stop")) {
            elapsedTime = System.currentTimeMillis() - startTime;
            timerHandler.removeCallbacks(timerRunnable);
            b.setText(R.string.resume);
        } else {
            startTime = System.currentTimeMillis() - elapsedTime;
            timerHandler.postDelayed(timerRunnable, 0);
             Calendar cs = Calendar.getInstance();
            System.out.println("Current time => " + cs.getTime());
            SimpleDateFormat df = new SimpleDateFormat("HH:mm");
            String formattedDate = df.format(cs.getTime());
            timerTextView.setText(formattedDate);
            b.setText(R.string.stop);
        }
    }
});

它将计算经过时间并显示停止后的时间...