Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JobService does not get rescheduled in android 9

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.

like image 977
Linus Nox Avatar asked Jan 21 '19 17:01

Linus Nox


People also ask

How do I start JobService on Android?

To create a Job Service, start by extending the JobService class and overriding 'onStartJob' and 'onStopJob'. OnStartJob is called by the Android system when it starts your job. If your task is short & simple,implement the logic directly in onStartJob() and return false when you are finished.

How does JobScheduler work in Android?

JobScheduler is introduced while the Android set limitation on background Execution. It means that the system will automatically destroy the background execution to save battery and memory. So if you want to perform some background job like some network operations no matter your app is running or not.

What is on start job called?

OnStartJob is called by the Android system when it starts your job. However, onStopJob is only called if the job was cancelled before being finished (e.g. we require the device to be charging, and it gets unplugged).


Video Answer


1 Answers

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.

like image 200
ankuranurag2 Avatar answered Sep 18 '22 15:09

ankuranurag2