-1

I am using following code to run a timer in my Android App.

I want to stop the Timer exactly when the time reaches to

  • 1 Minute
  • 2 Minute
  • 3 Minute

and so on. But I am not able to understand how to do it. Any Help Will be appreciated.

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    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;
            seconds = seconds % 60;

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

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

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

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

        Button b = (Button) findViewById(R.id.button);
        b.setText("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("start");
                } else {
                    startTime = System.currentTimeMillis();
                    timerHandler.postDelayed(timerRunnable, 0);
                    b.setText("stop");
                }
            }
        });
    }

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

}
Sandeep Londhe
  • 405
  • 1
  • 6
  • 17
  • hi, on SO there are many posts about timer Android. Just find them, Here an example: http://stackoverflow.com/questions/4597690/android-timer-how?rq=1 – piotrek1543 Jan 06 '16 at 10:57
  • @piotrek1543 you did not understand the question exactly , the question is how to stop the time, and all the answers are related to how to run the timer, hope you understand. – Sandeep Londhe Jan 06 '16 at 11:46

1 Answers1

2

Why not use the CountDownTimer class?

You can simply instantiate it as:

int bigTime = 1000000000;

//1000 is ms after which the timer ticks (that is, the method gets called and so, you can update your view)
CountDownTimer countDownTimer = new CountDownTimer(bigTime, 1000) {
    public void onTick(long millisUntilFinished) {
        updateTime(); 
        //you can write the code to update your view in this method
    }
    @Override
    public void onFinish() {
        Log.i("Get a life bro..."," 31 years have passed!");
    }
};

Now, in your onCreate() method, according to the clicklisteners on your start/stop button, you can simply start or stop the timer:

countDownTimer.start();

if(seconds == 0 && minutes > 0) {
    // get the values of seconds and minutes from the view.
    countDownTimer.cancel();
}

Just in case you want to pause the timer and start from the paused time, you can store the value of the time in milliseconds, stop the timer and restart it after adding the value you stored.

Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91