JobIntentService在Android 8.0上不能立即启动

15

我已经实现了JobIntentService来执行一些后台任务,这在旧版的Android设备上运行良好(Android O之前的版本)。我发现 intent 立即被处理,但在 Android O 设备上,在执行 JobIntentService.onHandleWork() 之前存在一些延迟。我知道意图是按顺序处理的,但即使队列中没有处理意图,我仍然看到延迟。这是因为 Android O 内部处理作业调度吗?

在这里,Android 文档说:

"当作为 pre-O 服务运行时,入队工作的行为通常会立即启动服务,而不管设备是否正在休眠或处于其他条件下。当作为 Job 运行时,它将受到具有 0 的 setOverrideDeadline(long) 的标准 JobScheduler 策略影响:当设备处于休眠状态时,该作业将不会运行,如果设备处于强内存压力下且有大量需求运行作业,则可能会延迟更长时间而不是服务。"

即使我的应用程序针对 API 25,但在 Android O 上运行,上述声明是否有效?如果是这样,是否有任何解决办法可以在 Android O 上立即启动服务/作业?

我的当前实现:

public class MySampleService extends JobIntentService {

    private static final int JOB_ID = 1000;

    public MySampleService() {

    }

    /**
     * Convenience method for enqueuing work in to this service.
     */
    public static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, MySampleService.class, JOB_ID, work);
    }

    /**
     * Interpret the incoming intent actions and handle it appropriately.
     * @param workIntent
     */
    @Override
    protected void onHandleWork(@NonNull Intent workIntent) {
        if(workIntent==null || workIntent.getAction() == null){
            return;
        }
        /* This gets printed immediately on pre Android O devices but not otherwise*/
        System.out.println("Intent Handled");
    }
}

我想知道它延迟了多久?是以秒、分钟还是小时计算的? - c0dehunter
对我来说,在它被显示之前花了一分钟,这似乎不是一个可靠的 IntentService 替代品(摆脱 WakefulBroadcastReceiver)。 - strangetimes
你的enqueueWork方法由哪个组件触发? - Florian Walther
2个回答

11
即使我的应用程序针对API 25,但在Android O上运行,上述语句是否有效? 是的。JobIntentService的行为-使用JobScheduler还是不使用-取决于您正在运行的Android版本。您的targetSdkVersion没有影响。
如果是这样,是否有任何解决方案可以立即在Android O上启动服务/作业? 不要使用JobIntentService。使用常规前台服务。根据定义,JobScheduler可以根据自己的判断安排作业,并且不能保证会立即执行作业。

谢谢CommonsWare。如果我要在onPause期间启动后台任务(我想在应用程序退出时发送一堆数据到网络),我应该使用JobIntentService还是只需启动常规的IntentService - Cheok Yan Cheng
是的,这项工作需要超过几秒钟的时间。我不确定是否使用IntentService,操作系统会将其杀死吗?ProcessLifecycleOwner对我来说是新事物。我的计划是在onPause中检查isFinishing - Cheok Yan Cheng
@CheokYanCheng: "不确定我是否使用IntentService,操作系统会杀死它吗?" -- 在Android 8.0+上,如果它运行超过一分钟,并且不是前台服务,则是的。 "我的计划是在onPause中检查isFinishing" -- 这适用于单个活动,而不适用于多活动应用程序的整个UI。 - CommonsWare
1
@NamrataBagerwal:一旦您接收到广播,您的BroadcastReceiver可以为JobIntentService排队一些工作。根据定义,一旦您接收到广播,您的应用程序正在运行。 - CommonsWare
1
@FlorianWalther:是的。这就是我在答案的后半部分所建议的。你需要支付的代价是“通知”,而在某些情况下,用户可能会珍视那个“通知”。 - CommonsWare
显示剩余19条评论

0

如果有人正在寻找一些数据:

测试设备:Nokia 6.1+
操作系统:Android 10

我已经为每1分钟位置更新安排了后台位置接收器,并从接收器启动了JobIntentService。在下面的日志中,您可以看到接收器被调用多次,并且工作也从接收器中多次入队。但是,JobIntentServiceonHandleIntent在近半小时后才被调用。尽管这种延迟可能在多种情况下有所不同。

1594846546292,2020.07.16 02:25:46.292,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594846760376,2020.07.16 02:29:20.376,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594847109044,2020.07.16 02:35:09.044,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594847509297,2020.07.16 02:41:49.297,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594847721898,2020.07.16 02:45:21.898,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594848071300,2020.07.16 02:51:11.300,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594848471389,2020.07.16 02:57:51.389,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594848685257,2020.07.16 03:01:25.257,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594849033283,2020.07.16 03:07:13.283,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594849433274,2020.07.16 03:13:53.274,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594849646987,2020.07.16 03:17:26.987,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594849995278,2020.07.16 03:23:15.278,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594850395353,2020.07.16 03:29:55.353,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594850609101,2020.07.16 03:33:29.101,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594850957287,2020.07.16 03:39:17.287,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594851521664,2020.07.16 03:48:41.664,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594851521668,2020.07.16 03:48:41.668,DEBUG,PRETTY_LOGGER,New Location = 19.2298493,72.824765
1594851521673,2020.07.16 03:48:41.673,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594851521675,2020.07.16 03:48:41.675,DEBUG,PRETTY_LOGGER,New Location = 19.2298482,72.8247637
1594851521677,2020.07.16 03:48:41.677,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594851521679,2020.07.16 03:48:41.679,DEBUG,PRETTY_LOGGER,New Location = 19.2298484,72.8247622
1594851521681,2020.07.16 03:48:41.681,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594851521682,2020.07.16 03:48:41.682,DEBUG,PRETTY_LOGGER,New Location = 19.2298492,72.8247653
1594851521686,2020.07.16 03:48:41.686,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594851521687,2020.07.16 03:48:41.687,DEBUG,PRETTY_LOGGER,New Location = 19.229849,72.8247665
1594851521690,2020.07.16 03:48:41.690,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594851521691,2020.07.16 03:48:41.691,DEBUG,PRETTY_LOGGER,New Location = 19.229849,72.8247664
1594851521694,2020.07.16 03:48:41.694,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594851521695,2020.07.16 03:48:41.695,DEBUG,PRETTY_LOGGER,New Location = 19.229849,72.8247664
1594851521698,2020.07.16 03:48:41.698,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594851521699,2020.07.16 03:48:41.699,DEBUG,PRETTY_LOGGER,New Location = 19.2298461,72.8247634
1594851521701,2020.07.16 03:48:41.701,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594851521702,2020.07.16 03:48:41.702,DEBUG,PRETTY_LOGGER,New Location = 19.2298493,72.824765
1594851521705,2020.07.16 03:48:41.705,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594851521706,2020.07.16 03:48:41.706,DEBUG,PRETTY_LOGGER,New Location = 19.2298492,72.8247653
1594851521709,2020.07.16 03:48:41.709,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594851521710,2020.07.16 03:48:41.710,DEBUG,PRETTY_LOGGER,New Location = 19.2298491,72.8247648
1594851521713,2020.07.16 03:48:41.713,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594851521715,2020.07.16 03:48:41.715,DEBUG,PRETTY_LOGGER,New Location = 19.2298482,72.824763
1594851521717,2020.07.16 03:48:41.717,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594851521718,2020.07.16 03:48:41.718,DEBUG,PRETTY_LOGGER,New Location = 19.2298489,72.8247663
1594851521721,2020.07.16 03:48:41.721,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594851521722,2020.07.16 03:48:41.722,DEBUG,PRETTY_LOGGER,New Location = 19.229849,72.824766
1594851521725,2020.07.16 03:48:41.725,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594851521727,2020.07.16 03:48:41.727,DEBUG,PRETTY_LOGGER,New Location = 19.2298492,72.824766
1594851521743,2020.07.16 03:48:41.743,DEBUG,PRETTY_LOGGER,destroying location update service..
1594851568410,2020.07.16 03:49:28.410,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594851568423,2020.07.16 03:49:28.423,DEBUG,PRETTY_LOGGER,Location Result is null
1594851569552,2020.07.16 03:49:29.552,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594851569582,2020.07.16 03:49:29.582,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594851569584,2020.07.16 03:49:29.584,DEBUG,PRETTY_LOGGER,Location Result is null
1594851842731,2020.07.16 03:54:02.731,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594851884757,2020.07.16 03:54:44.757,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594851966580,2020.07.16 03:56:06.580,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594852014859,2020.07.16 03:56:54.859,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594852099687,2020.07.16 03:58:19.687,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594852430624,2020.07.16 04:03:50.624,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594852430626,2020.07.16 04:03:50.626,DEBUG,PRETTY_LOGGER,New Location = 19.2298507,72.8247664
1594852430632,2020.07.16 04:03:50.632,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594852430636,2020.07.16 04:03:50.636,DEBUG,PRETTY_LOGGER,New Location = 19.2298511,72.8247665
1594852430639,2020.07.16 04:03:50.639,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594852430640,2020.07.16 04:03:50.640,DEBUG,PRETTY_LOGGER,New Location = 19.2298491,72.8247654
1594852430643,2020.07.16 04:03:50.643,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594852430645,2020.07.16 04:03:50.645,DEBUG,PRETTY_LOGGER,New Location = 19.229852,72.8247669
1594852430648,2020.07.16 04:03:50.648,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594852430650,2020.07.16 04:03:50.650,DEBUG,PRETTY_LOGGER,New Location = 19.229852,72.8247669
1594852430653,2020.07.16 04:03:50.653,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594852430655,2020.07.16 04:03:50.655,DEBUG,PRETTY_LOGGER,New Location = 19.2298517,72.8247667
1594852430664,2020.07.16 04:03:50.664,DEBUG,PRETTY_LOGGER,destroying location update service..
1594852532540,2020.07.16 04:05:32.540,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594852929377,2020.07.16 04:12:09.377,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594853035393,2020.07.16 04:13:55.393,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594853097587,2020.07.16 04:14:57.587,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594853518088,2020.07.16 04:21:58.088,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594853580673,2020.07.16 04:23:00.673,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594853641964,2020.07.16 04:24:01.964,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594853843544,2020.07.16 04:27:23.544,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594853904843,2020.07.16 04:28:24.843,DEBUG,PRETTY_LOGGER,Location updates receiver called
1594853947748,2020.07.16 04:29:07.748,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594853947777,2020.07.16 04:29:07.777,DEBUG,PRETTY_LOGGER,New Location = 19.2298533,72.8247706
1594853947781,2020.07.16 04:29:07.781,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594853947783,2020.07.16 04:29:07.783,DEBUG,PRETTY_LOGGER,New Location = 19.229852,72.8247669
1594853947786,2020.07.16 04:29:07.786,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594853947787,2020.07.16 04:29:07.787,DEBUG,PRETTY_LOGGER,New Location = 19.2298493,72.8247656
1594853947789,2020.07.16 04:29:07.789,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...
1594853947791,2020.07.16 04:29:07.791,DEBUG,PRETTY_LOGGER,New Location = 19.2298494,72.8247659
1594853947793,2020.07.16 04:29:07.793,DEBUG,PRETTY_LOGGER,Handling work in CTLocationUpdateService...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接