I am working on GPS tracking Application in which I need to track location after 15 seconds. I have use handler to start service from onStartCommand() from Service class. but After 15 second or after some time after it handler does not execute always. I have also used WakeLock to prevent app from sleeping. how to overcome this issue?
Asked
Active
Viewed 47 times
-1
-
Possible duplicate of [Using Handler inside service](https://stackoverflow.com/questions/48006889/using-handler-inside-service) – Adarsh Yashvanth Oct 08 '18 at 13:17
2 Answers
0
In short:
new Handler().postDelayed(() -> { ... }, 15000);
The action (lambda here) will be executed after the timeout (15,000 ms.)
Expanded it might look like this:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//Track my location
}
}, 15_000);

Nick Cardoso
- 20,807
- 14
- 73
- 124
-
I have done this way but it is not execute always after 15 second – Prosenjit Dhara Oct 08 '18 at 13:00
-
-
-1
Use Timer
instead of Handler
as shown below
new java.util.Timer().schedule(new java.util.TimerTask() {
@Override
public void run() {
//do your task here
}
}, 15000);

Adarsh Yashvanth
- 150
- 10
-
1Exactly why this is such a bad suggestion: https://stackoverflow.com/a/40339630/984830 – Nick Cardoso Oct 08 '18 at 13:09