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