gpt4 book ai didi

android - JobIntentService 不会在 Android 8.0 上立即启动

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:01:31 28 4
gpt4 key购买 nike

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

Here , Android文档说

"When running as a pre-O service, the act of enqueueing work will generally start the service immediately, regardless of whether the device is dozing or in other conditions. When running as a Job, it will be subject to standard JobScheduler policies for a Job with a setOverrideDeadline(long) of 0: the job will not run while the device is dozing, it may get delayed more than a service if the device is under strong memory pressure with lots of demand to run jobs."

即使我的应用以 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");
}
}

最佳答案

Is the above statement valid even if my app is targeted to API 25 but running on OS Android O?

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

If so, is there any workaround to start the service/job immediately on Android O?

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

关于android - JobIntentService 不会在 Android 8.0 上立即启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46856171/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com