JobIntentService无法在Android 8.0上立即启动

时间:2017-10-20 19:36:54

标签: android android-intent android-intentservice android-8.0-oreo

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

Here,Android文档说

  

“当作为pre-O服务运行时,排队工作的行为将会   一般都是立即启动服务,无论是否   设备是打瞌睡或在其他条件下。当作为工作运行时,它   将受制于具有a的Job的标准JobScheduler策略   setOverrideDeadline(long)为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");
    }
}

2 个答案:

答案 0 :(得分:2)

  

即使我的应用程序针对API 25但在OS Android O上运行,上述语句是否有效?

是。 JobIntentService行为 - 使用JobScheduler或不使用 - 取决于您运行的Android版本。您的targetSdkVersion没有任何影响。

  

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

请勿使用JobIntentService。使用常规前台服务。根据定义,JobScheduler可以按其认为合适的方式安排工作,并且无法保证它会立即执行工作。

答案 1 :(得分:0)

万一有人在寻找某些数据:

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

我已计划每1分钟更新一次background location receiver,并且从receiver开始JobIntentService。在下面的日志中,您可以看到Receiver被多次调用,并且job也从Receiver入队了多次。但是onHandleIntent中的JobIntentService在将近 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...