JobIntentService 不会在 Android 8.0 上立即启动

Posted

技术标签:

【中文标题】JobIntentService 不会在 Android 8.0 上立即启动【英文标题】:JobIntentService doesn't start immediately on Android 8.0 【发布时间】:2018-04-02 01:01:20 【问题描述】:

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

Here,Android 文档说

"当作为 pre-O 服务运行时,排队工作的行为将 一般会立即启动服务,不管是否 设备正在打瞌睡或处于其他状态。作为作业运行时,它 将受标准 JobScheduler 政策的约束 setOverrideDeadline(long) of 0:作业不会在设备运行时运行 正在打瞌睡,如果设备处于打瞌睡状态,它可能会比服务延迟更多 在强大的内存压力和大量运行作业的需求下。”

即使我的应用程序面向 API 25 但在 OS 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");
    

【问题讨论】:

我想知道它延迟了多长时间?是秒、分还是小时? 对我来说它花了一分钟才显示出来,这似乎不是 IntentService 的可靠替代品(远离 WakefulBroadcastReceiver) 哪个组件触发了你的 enqueueWork 方法? 【参考方案1】:

即使我的应用面向 API 25 但在 OS Android O 上运行,上述声明是否有效?

是的。 JobIntentService 行为 - 是否使用 JobScheduler - 取决于您运行的 Android 版本。你的targetSdkVersion 没有影响。

如果是这样,是否有任何解决方法可以立即在 Android O 上启动服务/作业?

不要使用JobIntentService。使用常规的前台服务。根据定义,JobScheduler 可以根据需要安排作业,但不能保证它会立即执行作业。

【讨论】:

感谢 CommonsWare。如果我要在onPause 期间启动后台任务(我想在应用退出期间通过网络发送一堆数据),我应该使用JobIntentService 还是只启动常规IntentService @CheokYanCheng:嗯,很可能两者都不是。除非这项工作可能需要几秒钟,否则只需执行其他操作(RxJava、Executor 等)。并且,对于“应用程序退出”,请使用 ProcessLifecycleOwner,而不是 onPause(),因为这被称为很多,尤其是在多窗口场景中。如果工作可能超过几秒钟,IntentServiceJobIntentService 之间的选择归结为即时性、工作可能需要多长时间,以及在工作进行时是否可以显示Notification。任何一种方法都可能是正确的;视情况而定。 是的。该作业将花费几秒钟以上的时间。不确定我是否使用IntentService,操作系统会杀死它吗? ProcessLifecycleOwner 对我来说是新事物。我的计划是在onPause 中检查isFinishing @NamrataBagerwal:嗯,一旦你收到广播,你的BroadcastReceiver 可以为JobIntentService 排队。根据定义,一旦您收到广播,您的应用就会运行。 @FlorianWalther:是的。这就是我在回答的后半部分提出的建议。您必须付出的代价是Notification,在某些情况下,用户可能会重视Notification【参考方案2】:

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

测试设备:诺基亚 6.1+操作系统:Android 10

我有background location receiver 计划每 1 分钟更新一次位置,从receiver 我开始JobIntentService。在下面的日志中,您可以看到Receiver 被多次调用,并且作业也多次从Receiver 入队。但是 onHandleIntentJobIntentService 在几乎 1/2 小时后被调用。尽管此延迟在多种情况下可能会有所不同。

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...

【讨论】:

以上是关于JobIntentService 不会在 Android 8.0 上立即启动的主要内容,如果未能解决你的问题,请参考以下文章

JobIntentService 不适用于 Oreo 以下的 API (API <= 26)

Activity 和 JobIntentService 生命周期

来自 JobIntentService 的吐司

JobIntentService 的 onDestroy 被调用后会发生啥?

在 JobIntentService 中可以在没有 onHandleWork() 的情况下调用 onDestroy() 吗?

Kotlin:如何从 Fragment 调用 JobIntentService?