I have a Service in my application which is designed to run every 10 minutes. It basically checks up on our servers to see if everything is running properly and notifies the user of any problems. I created this application for internal use at our company.
My co-worker used the application over the long weekend and noticed that no checks were performed when the device went to sleep. I was under the impression that the Service was supposed to keep running in the background until I explicitly call stopService()
in my code.
So ultimately, my goal is to have the service running until the user hits the off button in the application or kills the process.
I heard about something called WakeLock
which is meant to keep the screen from turning off, which is not what I want. I then heard of another thing called a partial WakeLock, which keeps the CPU running even when the device is asleep. The latter sounds closer to what I need.
How do I acquire this WakeLock and when should I release it and are there other ways around this?
Note: This post has been updated to include the
JobScheduler
API of the Android Lollipop release. The following is still a viable way, but can be considered deprecated if you're targeting Android Lollipop and beyond. See the second half for theJobScheduler
alternative.One way to do recurrent tasks is this:
Create a class
AlarmReceiver
with
YourService
being your service ;-)If you require a wake lock for your Task, it is advisable to extend from
WakefulBroadcastReceiver
. Don't forget to add theWAKE_LOCK
permission in your Manifest in this case!To start your recurrent polling, execute this code in your activity:
This code sets up an
alarm
and a canceablependingIntent
. ThealarmManager
gets the job to repeat therecurringAlarm
every day (third argument), but inexact so the CPU does wake up approximately after the interval but not exactly (It lets the OS choose the optimal time, which reduces battery drain). The first time the alarm (and thus the service) is started will be the time you choose to beupdateTime
.last but not least: here is how to kill the recurring alarm
This code creates a copy of your (probably) existing alarm and tells the
alarmManager
to cancel all alarms of that kind.Manifest
:include these two lines
inside the
< application>
-tag. Without it, the system does not accept the start of recurrent alarm of a service.Starting with the Android Lollipop release, there's a new way of solving this task elegantly. This also makes it easier to only perform an action if certain criteria such as network state are met.
You may also provide a deadline with
setOverrideDeadline(maxExecutionDelayMillis)
.To get rid of such a task, just call
jobScheduler.cancel(mJobId);
orjobScheduler.cancelAll();
.From Android Documentation in doze mode following happens: (https://developer.android.com/training/monitoring-device-state/doze-standby):
The system ignores wake locks.
The system does not allow JobScheduler to run.
Android ignores AlarmManager as well unless they are in
setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
Network access is suspended.
So the only way is to use FCM on high priority or AlarmManager with setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
I would have recommended, if building this application from the beginning to use a server-side component (yes, would also need monitoring!) and send push notifications, polling is never a reliable solution.