In my application I have a service that is querying a server for data.
This service queries the server every x minutes, which can be configured.
When I run the application, connected to USB cable, the
timer.schedule(new MyTimerTask(), delayInSec, TimeUnit.SECONDS);
works perfectly. But when the device not connected to USB (computer), it stopped.
It returns to work when device is re connected to USB.
How it can be solved?
Thanks,
Eyal.
Step #1: Delete your existing
ScheduledExecutorServiceimplementation.Step #2: Use
AlarmManagerto set up your scheduled work, having the work be implemented by aWakefulIntentService, so the device will wake up to do the work, stay awake long enough to complete the work, then go back to sleep when done (and let your service shut down in between jobs, so it does not tie up the user’s RAM constantly).