My app uses this classic pattern to schedule periodic tasks:
- Set an exact alarm via
AlarmManager(viasetExactAndAllowWhileIdle(), since it has to go off even in Doze) - Start an
IntentServicefromonReceive()viaWakefulBroadcastReceiver.startWakefulService() - Do the work in
onHandleIntent()and callWakefulBroadcastReceiver.completeWakefulIntent()when finished.
Today i updated the targetSdkVersion to 26 and faced the terrible fact that WakefulBroadcastReceiver is deprecated.
I went to read API docs immediately and found the following:
As of Android O, background check restrictions make this class no longer generally useful. (It is generally not safe to start a service from the receipt of a broadcast, because you don't have any guarantees that your app is in the foreground at this point and thus allowed to do so.) Instead, developers should use
android.app.job.JobSchedulerto schedule a job, and this does not require that the app hold a wake lock while doing so (the system will take care of holding a wake lock for the job).
This is kinda confusing to me, i don't really understand what is the purpose of AlarmManager.setExactAndAllowWhileIdle() without being able to keep the device awake anymore.
As i see i cannot set an exact time to run a job with JobScheduler, only conditions (such as network type or charging state) so i have no idea what to do here.
I thought of either
Using
AlarmManagerandJobSchedulertogetherSet an alarm (with
setExactAndAllowWhileIdle()) and start a job (viaJobScheduler) immediately fromonReceive(). SinceJobSchedulerprovides aWakeLock,WakefulBroadcastReceiveris not needed.(does this make sense?)
or
- Keep using
WakefulBroadcastReceiverdespite being deprecated.
I'd really appreciate any advice on this matter.