简体   繁体   中英

Resuming & Pausing a timer based on handler

I have set a timer and it display the time counted on the application screen. When you tap the start button the timer is running and when you tap stop it stops, though when I'm trying to resume it, it restarts the existing time.

How can I resume the timer without refreshing/restarting the existed time?

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

try this code:

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

it will calculate the elapsed time and show time after stop...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM