onStartJob not getting called on API 30

Viewed 90

I have a requirement to create a channel on android TV after apk is installed but before app is first launched. So as soon as apk is installed i need to run a piece of code to create the channel on device.

For this, I wrote a receiver that schedules a Jobservice.

    class MyIntentReceiver: BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        NotificationJobService.schedule(context!!)
    }
}


    class NotificationJobService : JobService() {

    companion object {
        fun schedule(context: Context) {
            val jobService = ComponentName(context, NotificationJobService::class.java)
            val jobInfo = JobInfo.Builder(501, jobService)
                .setMinimumLatency(0)
                .setOverrideDeadline(0)
                .build()
            val result = (context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler) .schedule(jobInfo)
            Log.d("DEBUG_TAG", "Job scheduled with. $result")
        }
    }

    override fun onStartJob(params: JobParameters?): Boolean {
        Log.d("DEBUG_TAG", "onStartJob Called.")
        return true
    }

    override fun onStopJob(params: JobParameters?): Boolean {
        Log.d("DEBUG_TAG", "onStopJob Called.")
        return true
    }
}

This code works as expected on Android 27 , 28. i.e after the apk is installed (before first launch of the app), if i use this command

adb shell am broadcast -a android.media.tv.action.INITIALIZE_PROGRAMS -n com.projects.test.scheduler/com.projects.test.scheduler.MyIntentReceiver

I see

Job scheduled with 1
onStartJob Called.

However, on Android 29 & 30 i only see one debug statement

Job scheduled with 1

To test further, if i follow below steps

  1. Install apk.
  2. Start app.
  3. Kill app.
  4. Run above adb command to fire an intent

then i can I see both log messages being printed

Job scheduled with 1
onStartJob Called.

Any ideas how why it is behaving this way on 29 & 30?

0 Answers
Related