6

I'm trying to get my app working on Android 9. The following code works fine up to Android 8, but for some reason, the JobService does not get rescheduled on android 9. It gets scheduled the first time but does not get rescheduled according to the set periodic.

class RetrieveJobService : JobService() {

override fun onStartJob(params: JobParameters): Boolean {
    doBackgroundWork(params)
    return true
}

private fun doBackgroundWork(params: JobParameters) {
    Thread {
        try {
            doRetrieveBackgroundStuff(this)
            jobFinished(params, false)
        } catch (e: Exception) {
            jobFinished(params, false)
        }
    }.start()
}

override fun onStopJob(params: JobParameters): Boolean {
    return false
}

}

And here my JobInfo.Builder

val builder = JobInfo.Builder(jobID, componentName)
                    .setPersisted(true)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    builder.setPeriodic(millis, 15 * 60 * 1000) //15 min
} else {
    builder.setPeriodic(millis)
}
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)

val scheduler = context.getSystemService(JOB_SCHEDULER_SERVICE) as 
        JobScheduler
val resultCode = scheduler.schedule(builder.build())

Any ideas? EDIT: Just to be clear, this code worked fine on Android 8 and below and does also work on the Android Studio emulator running Android 9. As far as I can test, it does not work on any physic device running Android 9.

Linus Nox
  • 67
  • 1
  • 6
  • Does it not works when you have closed the app or never works.Also what is the Device Manufacturer name? – ankuranurag2 Jan 23 '19 at 11:13
  • It never works when die app is closed, I have seen it working when the app is opened after the specified periodic time interval but it is not consistent. Device: Huawei P20, OnePlus 6T. Emulator: Nexus 5X – Linus Nox Jan 23 '19 at 11:38
  • Many of the Device manufactures like OnePlus,Oppo,Huwaei,Redmi,Vivo etc force close the app when it is cleared from recent tabs. Thus, no scheduled task or alarm manager will work for the app. https://issuetracker.google.com/issues/110745313 – ankuranurag2 Jan 23 '19 at 11:46

1 Answers1

8

If you go through THE LINK, you will find:

Unfortunately, some devices implement killing the app from the recents menu as a force stop. Stock Android does not do this. When an app is force stopped, it cannot execute jobs, receive alarms or broadcasts, etc. So unfortunately, it's infeasible for us to address it - the problem lies in the OS and there is no workaround.

It is a known issue. To save battery, many manufacturers force close the app, thus cancelling all the period tasks, alarms and broadcast recievers etc. Major manufacturers being OnePlus(you have option to toogle),Redmi,Vivo,Oppo,Huwaei.

UPDATE

Each of these devices have AutoStartManagers/AutoLaunch/StartManager type of optimization managers. Which prevent the background activities to start again. You will have to manually ask the user to whitelist your application, so that app can auto start its background processess. Follow THIS and THIS link, for more info.

The methods to add to whitelist for different manufactures are give in this stackoverflow answer. Even after adding to whitelist, your app might not work because of DOZE Mode, for that you will have to ignore battery otimizations

Also in case you might be wondering, apps like Gmail/Hangout/WhatsApp/Slack/LinkedIn etc are already whitelisted by these AutoStart Managers. Hence, there is no effect on their background processes. You always receive timely updates & notifications.

ankuranurag2
  • 2,300
  • 15
  • 30
  • Thank you for your response! I'm wondering how I should implement my app then. To give more context. I'm working on a wallpaper app that automatically sets a new wallpaper every few hours. I thought JobService is the right tool used for those tasks. – Linus Nox Jan 23 '19 at 16:14
  • If you have any other doubt, feel free to ask. – ankuranurag2 Jan 24 '19 at 11:20
  • 2
    Just want to echo @LinusNox; it's a year later and I'm having similar issues with JobScheduler. Somebody should tell Google that, from a developer's perspective, it's extremely frustrating to be told we should start using JobScheduler now, only to find that it's been locked down to the point where it's all but unusable (unless you happen to be Google or Facebook and are pre-whitelisted). I understand wanting to preserve battery life, but this is just ridiculous. /rant – Kris Craig Jan 17 '20 at 08:19