JobIntentService-不总是调用onHandleWork吗? [英] JobIntentService - onHandleWork is not always called?

查看:153
本文介绍了JobIntentService-不总是调用onHandleWork吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主屏幕小部件,其中有一个简单的 AppWidgetProvider JobIntentService ,我将在其中完成所有工作.

I have home screen widget, which has a simple AppWidgetProvider and JobIntentService, where I do all the work.

问题是-它是随机产生的.有时确实如此,有时却没有-最奇怪的是,我在日志中看到,总是调用 JobIntentService 的每个小部件更新 enqueueWork 方法,但是onHandleWork 方法只是有时.

Problem is - it works kind of randomly. Sometimes it does, sometimes it doesnt - weirdest thing is, I can see in the log, that on each widget update enqueueWork method of JobIntentService is ALWAYS called, but onHandleWork method only sometimes.

(我发现与电池优化的相关性很强,尽管不是100%相关.如果我打开自动管理应用程序",那么它有99%可靠地工作.如果打开,就像掷硬币一样.最能说明问题的方法是这段简短的视频

(I have found there is strong, though not 100% correlancy with battery optimization. If I turn of "Manage apps automatically", then it 99% works reliably. If it is turned on, its like flipping a coin. Sometimes it does, sometimes it doesnt. Best to ilustrate behavior would be this short simple video

这是我的代码(窗口小部件提供程序):

This is my code (Widget provider):

public class MyWidgetProvider extends AppWidgetProvider {
    
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.v("aaa", "onUpdate");
        // super.onUpdate(context, appWidgetManager, appWidgetIds);

        // update in my own Service (to avoid ANR)
        Intent intent = new Intent(context, MyJobIntentService.class);
        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        MyJobIntentService.enqueueWork(context, intent);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v("aaa", "onReceive");

        super.onReceive(context, intent);
    }
}

这是我所有工作的服务(JobIntentService):

And this is my service (JobIntentService) where I do all work:

public class MyJobIntentService extends JobIntentService {

    public static final int JOB_ID = 1;

    public static void enqueueWork(Context context, Intent work) {
        Log.v("aaa", "enqueueWork: ");
        enqueueWork(context, MyJobIntentService .class, JOB_ID, work);
    }

    @Override
    protected void onHandleWork(Intent intent) {
        Log.v("aaa", "onHandleWork: ");
    }
    
}

推荐答案

这与我发生的事情相同.我发现的是,在android文档中,它说:

This is the same thing that is happening to me. What I've found out is that in the android documentation, it says:

作为pre-O服务运行时,入队工作将通常,无论是否设备正在打zing睡或处于其他条件下.作为作业运行时,它对于具有以下条件的作业,将遵循标准的JobScheduler政策:JobInfo.Builder.setOverrideDeadline(long)为0:作业将不会运行当设备在打if睡时,它可能比服务延迟得更多,如果该设备承受着巨大的内存压力,需要大量运行工作.

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

这意味着在Android O及更高版本的设备中,不能保证jobIntentService立即启动,而是根据设备的状况从延迟开始.但是,一些电话制造商故意取消这项工作以延长电池寿命(例如OnePlus).因此,这项工作永远不会被召唤.

This means in Android O and above devices, it is not guaranteed that the jobIntentService starts immediately, but start with the delay depending on the device's condition. But, some phone manufacturers are deliberately canceling the job for better battery life (Like OnePlus). So the job is never called.

我使用的解决方法是创建LOW_IMPORTANCE前台服务并使用以下命令调用它:

The workaround that I used is to create a LOW_IMPORTANCE foreground service and call it using:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    context.startForegroundService(startIntent);
} else {
    context.startService(startIntent);
}

但是缺点是通知显示的时间很短,直到工作完成.

But the drawback is that the notification is shown for a small duration till the work is complete.

也许google应该在设备制造商上对如何管理后台应用程序施加一些限制.

Maybe google should put some restrictions on the device manufacturers on how to manage the background apps.

这篇关于JobIntentService-不总是调用onHandleWork吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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