10

Can i use this code inside a service for calling a method with delay or Handler() should only be used inside a UI thread ?

What is the best way for calling a method with delay inside a service?

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    //Do something after 10000ms
    socket.emit("CancelTravel");
  }
}, 10000);
Goku
  • 9,102
  • 8
  • 50
  • 81
Milad ranjbar
  • 569
  • 1
  • 8
  • 17
  • You should decide that based on the usecase of your app. [See cgr´s post here,to decide if you should use a normal handler or an alarm manager](https://stackoverflow.com/a/26740872/9128745) – martin Dec 28 '17 at 11:55

4 Answers4

10

Handler() only should be use inside a UI thread ?

Yes Handler() usefull only on UI thread and if you want use on normal thread, you need to implement looper

Sample code

    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            //Do something after 10000ms
            socket.emit("CancelTravel");

        }
    }, 5000);

You can also use Timer

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.

Sample Code

new Timer().schedule(new TimerTask() {          
    @Override
    public void run() {
        //Do something after 10000ms
    socket.emit("CancelTravel");       
    }
}, 10000);

what is preferred way for calling a method with delay inside a service?

Read Timertask or Handler

Goku
  • 9,102
  • 8
  • 50
  • 81
  • 4
    `"Yes Handler() usefull only on UI thread"` its not true - it can be used with any thread with attached `Looper` - for example `HandlerThread` – pskink Dec 28 '17 at 11:54
  • @Prem thanks man, this code you suggested will be run one time, or repeated?, i just want run one time. – Milad ranjbar Dec 28 '17 at 11:54
1

You can use delay in Service like this:

private Handler handler = null;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    handler = new Handler();
    ...
}

...

    // from inside your child thread
    handler.post(new Runnable() {
        @Override
        public void run() {
           //your code goes here
        }
    });
Abubakker Moallim
  • 1,610
  • 10
  • 20
1

//this code can be used to set delay for 10 seconds. Yes it can be used

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    //Do something after 10000ms
    socket.emit("CancelTravel");
  }
}, 10000);
Mohit Kalia
  • 351
  • 1
  • 6
  • you right i tested and it's works, because when Handler is inside a service it will derived from service thread and not ui thread, is it correct? – Milad ranjbar Dec 28 '17 at 12:04
  • @Miladranjbar Yes Handler() usefull only on UI thread and if you want use on normal thread, you need to implement looper – Goku Dec 28 '17 at 12:14
0

you can just make sure you have taken handler instance on main thread so that you can able run post() runnable on Main Thread.

you can do that by just little modification in your code, shown below.

final Handler handler = new Handler(Looper.getMainLooper());