JobService不会在Android 9中重新安排 [英] JobService does not get rescheduled in android 9

查看:381
本文介绍了JobService不会在Android 9中重新安排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的应用程序在Android 9上运行.以下代码在Android 8上正常运行,但是由于某些原因,JobService在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
}

}

这是我的JobInfo.Builder

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())

有什么想法吗? 请注意,此代码在Android 8及更低版本的Android 8上运行良好,并且在运行Android 9的Android Studio模拟器上也可以正常运行.据我测试,它在运行Android 9的任何物理设备上均不起作用. >

Any ideas? 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.

推荐答案

如果您通过 THE LINK ,您会发现:

If you go through THE LINK, you will find:

不幸的是,某些设备实施了从最近"菜单中杀死应用程序的强制停止操作. Stock Android不会执行此操作.当某个应用被强制停止时,它无法执行作业,接收警报或广播等.因此,不幸的是,我们无法解决这个问题-问题出在操作系统上,没有解决方法.

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.

这是一个已知问题.为了节省电池,许多制造商强行关闭了该应用程序,从而取消了所有定期任务,警报和广播接收器等.主要制造商为OnePlus(您可以选择切换),Redmi,Vivo,Oppo,Huwaei.

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.

更新

这些设备中的每一个都具有优化管理器的AutoStartManagers/AutoLaunch/StartManager类型.这会阻止后台活动再次开始.您将必须手动要求用户将您的应用程序列入白名单,以便该应用程序可以自动启动其后台进程.遵循链接,以获取更多信息.

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.

要添加到不同制造商白名单的方法,请参见此stackoverflow答案.即使添加到白名单之后,由于打might"模式,您的应用也可能无法运行,因为您必须以编程方式

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

如果您可能想知道,这些自动启动管理器已经将Gmail/Hangout/WhatsApp/Slack/LinkedIn等应用列入了白名单.因此,对其背景过程没有影响.您总是会及时收到更新信息.通知.

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.

这篇关于JobService不会在Android 9中重新安排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆