0

I have a button(in say Activity 1), which when clicked should start a service (eg Service 1). But there must be a delay of 5 seconds before the service starts. I achieved this using SystemClock.sleep(5000) in the onStartCommand of the service. This worked properly.

Now I want to add the functionality that if the button is clicked again(even before the 5 seconds end), the service WILL NOT BE STARTED.

Any ideas how to do this?

(Edit : Please read the entire question before marking it as a duplicate. Thanks)

Siddharth Sharma
  • 310
  • 1
  • 2
  • 11
  • Even you start a service multiple times, there will be just 1 instance of it, so you don't need to worry – HendraWD Dec 06 '16 at 04:06
  • Possible duplicate of [How to call a method after a delay in Android](http://stackoverflow.com/questions/3072173/how-to-call-a-method-after-a-delay-in-android) – Nic Robertson Dec 06 '16 at 04:11
  • @NicRobertson No it's not a delay because of the requirement that the 5 sec time lapse and the start of service should not occur if the user presses the button again. – Siddharth Sharma Dec 06 '16 at 04:14
  • @SiddharthSharma still fairly similar, but I have added an answer either way. – Nic Robertson Dec 06 '16 at 04:18

4 Answers4

0

You can use handler with post delayed to achieve your goal. Make your button disable and enable it after five seconds along with starting your service. You can implement the following code:

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                button.setEnabled(false);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //start your service here
                        button.setEnabled(true);
                    }
                }, 5000);
            }
        });

Above code will disable your button for 5 second and will start your service after 5 second.

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40
  • it the situation occurs that the user clicks the button within the 5 sec interval, the service should not be started(which cannot be achieved by disabling the button). – Siddharth Sharma Dec 06 '16 at 03:41
  • @SiddharthSharma See if button is disabled for 5 sec and the only way to start your service is to hit the button, how can you start service if button is not accepting any click? Try to understand the flow. I think above code will get your work done. – Rahul Sharma Dec 06 '16 at 03:45
  • The issue is that I don't want to disable the button at all. Just a 5 sec gap between button clicking and service starting. If button is clicked again, either service is not started(time < 5sec) or service is stopped(time > 5sec). – Siddharth Sharma Dec 06 '16 at 03:54
  • @SiddharthSharma Correct me if I am wrong, You mean if the user again clicks on the button within 5 second nothing should happen and if user clicks after 5 second service should stop? – Rahul Sharma Dec 06 '16 at 03:59
0

I'd use a util class similar to the following. Pass it in a runnable and a delay in ms and you can call stop() on it to cancel before it has run. You can also call restart() if you want to restart your timer. I use it for things like auto showing/hiding controls on an immersive view.

public class DelayableRunnable{

  int mDelay = 0;
  Handler mHandler = new Handler();
  Runnable mRunnable;
  boolean mIsRunning = false;

  public DelayableRunnable(Runnable runnable, int delay){
      mRunnable = runnable;
      mDelay = delay;
  }

  public void setNewDelay(int delay){
      mDelay = delay;
  }

  public void start(){
      if(mIsRunning) {
          stop();
      }
      mHandler.postDelayed(mRunnable, mDelay);
      mIsRunning = true;
  }

  public void stop(){
      mHandler.removeCallbacks(mRunnable);
      mIsRunning = false;
  }

  public void restart(){
      stop();
      start();
  }
}
Nic Robertson
  • 1,198
  • 9
  • 26
0

You can use Handler.postDelayed function for delayed actions in Android enviroment (better than plan java methods)

final Handler handler = new Handler(); // or use existed one your_view.getHandler()
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        //start your service
    }
}, 5000 /* 5s * 1000ms */);

Or simpler use you view function (work same as above):

your_view.postDelayed(new Runnable() {
    @Override
    public void run() {
        //start your service
    }
}, 5000 /* 5s * 1000ms */);
Hossain Khademian
  • 1,616
  • 3
  • 19
  • 29
0

A facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals void schedule (TimerTask task,long delay) Schedules the specified task for execution after the specified delay.

 new Timer().schedule(new TimerTask() {
        @Override
        public void run() {

            alertDialog.dismiss();
            startActivity(new Intent(****.this,*********.class));

        }
    },5000);
Mallikarjuna
  • 874
  • 6
  • 17
  • 3
    Please add a description to describe the block of code you have posted. It makes it easier for the community and OP to understand what you have asked. – BusyProgrammer Mar 05 '17 at 17:17
  • A facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals void schedule (TimerTask task,long delay) Schedules the specified task for execution after the specified delay. – Mallikarjuna Mar 05 '17 at 18:27
  • It would be great if you edited your post to include the description you just posted here as a comment. – BusyProgrammer Mar 05 '17 at 19:10