I am working on making my alarm app compatible with a soon to be released Android 12 and lately I stumbled upon a problem. It seems that since Android 12 update, jobs scheduled in Job Scheduler during locked/direct boot are not started until after the device is unlocked. I have reviewed changes made in Android 12 and I don't find any that would apply to my situation and I found out about it during random tests.
It is a major problem, as I need to reschedule alarms for my app on devices reboot and can't wait for the user to unlock the device first, as a reboot may happen automatically overnight, leaving user without a scheduled alarm in the morning.
When running below test app (targeting Android 11), results are as below:
- Android 11 and lower: job is scheduled and started immediately both during locked and regular boot.
- Android 12: work as above during regular boot, but in locked boot job is scheduled immediately, but starts only after device is unlocked.
Any suggestions on how to work with this, beside running whole job manually, for example with a use of a WakeLock? Or maybe someone knows what changes of Android 12 are actually in effect here?
Here's a simple test class for observing this situation:
class TestScheduler : JobService() {
companion object {
fun addScheduledJob(context: Context){
JobInfo.Builder(1111, ComponentName(context, TestScheduler::class.java))
.setOverrideDeadline(TimeUnit.SECONDS.toMillis(1))
.build().let {
val result = (context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler).schedule(it)
when(result){
JobScheduler.RESULT_SUCCESS -> "success"
else -> "failure"
}.also {
Log.d("MyTAG", "jobScheduled: $it")
}
}
}
}
override fun onStartJob(params: JobParameters?): Boolean {
Log.d("MyTAG", "onStartJob: ${params?.jobId}")
jobFinished(params, false)
return true
}
override fun onStopJob(params: JobParameters?): Boolean {
Log.d("MyTAG", "onStopJob: ${params?.jobId}")
return true
}
}
Job is scheduled from a boot receiver:
class OnBootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
TestScheduler.addScheduleJob(context)
}
}
Both clasess are directBootAware (manifest's content):
<receiver
android:name=".OnBootReceiver"
android:directBootAware="true"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".TestScheduler"
android:directBootAware="true"
android:permission="android.permission.BIND_JOB_SERVICE" />
from Job Scheduler not starting jobs in locked/direct boot on Android 12
No comments:
Post a Comment