-4

So, in my application, it's like ,

  1. A task is to performed within a given time interval
  2. If the task is performed within the timer's time limit , then i want to add say t seconds to the timer
  3. Then again i want to perform step 2.
  4. If the task is not performed within the timer's time limit , then the application will say, something like "Sorry, time's up".

How to do it? Please help me with required APIs and how to use them.

  • `Handler.postDelayed()`? And maybe also `removeCallbacks` or whatever it was so that you can re-add the thing with different delay. – EpicPandaForce Jul 30 '17 at 16:24

2 Answers2

1

You can use a CountDownTimer, check here for more details : Android documentation for CountDownTimer

Dilpreet Singh
  • 101
  • 1
  • 8
  • It explains how to perform a task within a time interval. But how to add more time to the timer ? – Pratik Kejriwal Jul 31 '17 at 17:03
  • Use a variable say `countDownTime` with initial value for countdown in the CountDownTimer constructor and update `countDownTime` with new value in `onFinish()` and start it again as per your requirement – Dilpreet Singh Jul 31 '17 at 18:43
0

use CountDownTimer for to take aaction after time is over

new CountDownTimer(20000, 1000) {

    public void onTick(long millisUntilFinished) {
       textView.setText("seconds remaining: " + millisUntilFinished / 1000); 
    }

    public void onFinish() {
        textView.setText("succcess"); //or textView.setText("Failed");
    }

}.start();
Mahesh Gawhane
  • 348
  • 3
  • 20