2

I have a button on which I want to set the timer for 5 seconds for the first time and it should perform some task after completing 5 seconds. Also if user click button 2 times it should start timer for 10 seconds and after 10 seconds it should perform specific task. and if user click 3rd time it should stop all running timers. so I have do not know How to implement timer for one time what I have search is this. But in this link it is continuously repeating after specific period of time, whereas I want to run once.

Now what I want

  • To start timer with first click (of 5 seconds)and if meanwhile user click 2nd time it should set timer with with new time period and if user click third time it cancels out all timers.
  • I do not want to use Thread timer using sleep method.
  • I want same behavior as there is in camera app in android 5.0 v.

So please tell me how to do this any code and source code would be appreciated.

Community
  • 1
  • 1
ghost talker
  • 402
  • 5
  • 15

1 Answers1

7

In the link you provided you will find the answer if you try little harder.

For a repeating task:

new Timer().scheduleAtFixedRate(task, after, interval);

For a single run of a task:

new Timer().schedule(task, after);

So what you need to do is to maintain temporary variable to keep track of number of clicks and you can use second method like

For a repeating task:

new Timer().scheduleAtFixedRate(task, after, interval);

For a single run of a task:

new Timer().schedule(task, after * numberOfTimeBtnClked);

You have to pass the TimerTask method instead of task in that method.

**For updating your textview use below code and forget about whatever I have written above **

public void startTimer() {
       //set a new Timer
       timer = new Timer();

       //initialize the TimerTask's job
       initializeTimerTask();

       //run in an interval of 1000ms 
       timer.schedule(timerTask, 0, 1000); //
}

public void initializeTimerTask() {

        timerTask = new TimerTask() {
                public void run() {

                    handler.post(new Runnable() {
                        public void run() {
                            timerSince++; //global integer variable with default value 0
                            if(timerSince == 5 * numberOfBtnClick){
                                //call your method
                                timer.cancel;
                                timerSince = 0;
                            }else{
                                //textView.setText(((5 * numberOfBtnClick)-timerSince)+" second left");
                            }
                        });
                    }
                };
        }
}

On event start button click call:

startTimer();
Kunu
  • 5,078
  • 6
  • 33
  • 61
  • I will try it , But can you tell me , will it run on Ui Thread – ghost talker Apr 07 '15 at 11:36
  • No it will run on different thread. TimerTask will run on different thread. If you wan't it to run on your main thread then use `runOnUiThread `. – Kunu Apr 07 '15 at 11:41
  • you are right. Now I want to show the time by time seconds on screen , I mean A count down counting numbers to show how many second are left., so how Can I do this? – ghost talker Apr 07 '15 at 12:02
  • I Know the Toast but I do not want to use it . Is there anyother way – ghost talker Apr 07 '15 at 12:03
  • Use a `TextView` and setText inside the timer – Kunu Apr 07 '15 at 12:28
  • I mean 10,9,8,7,,,,,1 in this way – ghost talker Apr 07 '15 at 12:53
  • use a timer that will run on every second and set your TextView. And in that same timer you can check when it is 5 second or 10 second launch your method. – Kunu Apr 07 '15 at 13:35
  • I didnt get your point but thanks anyways. It would be more appropriate if you share me a link of same thing i want . But thanks BTW – ghost talker Apr 07 '15 at 13:54
  • I will try to do that or some code snippet once I get some time today. – Kunu Apr 07 '15 at 14:30
  • did you get some time today Kunu – ghost talker Apr 08 '15 at 06:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74696/discussion-between-kunu-and-ghost-talker). – Kunu Apr 08 '15 at 07:17
  • I slightly changed your code , As you didnt got my idea , actually I want unable to make you fully understand. My bad , But thanks – ghost talker Apr 08 '15 at 09:34
  • timer.schedule(timerTask,timeDelay, 1000); making changes here , I am putting 5 or 10 in timedelay , so its 5*1000 would run for 5 sec and after completion I am taking my action and then also making my timer stop , thats it – ghost talker Apr 08 '15 at 09:36