自定义JobIntentService onHandleWork未调用

问题描述 投票:26回答:10

我最近正在更新一个应用程序,我使用JobIntentService而不是常规的IntentService处理来自push的通知,因为它似乎是在棒棒糖前设备和帖子上处理此问题的正确方法。我这样排队工作:

enqueueWork(context, MyJobServiceExtension.class, JOB_ID, work);

这是清单声明:

<service android:name="com.example.MyJobServiceExtension" android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true" tools:node="replace">

我从未在onHandleWork中看到任何回调或在我的logcat中看到任何错误日志。有没有人成功整合这可以帮助?

更新:我在api level 21设备上测试了它并且它工作..但它似乎没有在我的android O像素XL设备上调用..任何线索为什么?

更新#2:我似乎也看到了intentservice的onCreate被调用,但没有其他生命周期方法(包括onHandleWork)。有人遇到过这个吗?

android android-8.0-oreo jobservice
10个回答
54
投票

从IntentService升级到JobIntentService后,我遇到了同样的问题。确保从旧实现中删除此方法:

@Override
public IBinder onBind(Intent intent) {
    return null;
}

对我来说,这解决了这个问题,现在它在奥利奥之前和之后都有效。


0
投票

我终于找到了解决这个问题的解决方案

如果覆盖“onBind”方法并使用“enqueueWork”方法调用工作,则需要将绑定返回到执行此操作的引擎:

@Override @Nullable
    public IBinder onBind(@NonNull Intent intent) {
        [... Do What You Want ... ]
        return super.onBind(intent);
    }

因此返回“super.onBind”方法的IBinder,因此必须使用它来绑定到JobIntentService。

如果你想绑定并返回另一个绑定器,你可以这样做:

@Override @Nullable
    public IBinder onBind(@NonNull Intent intent) {
        IBinder binder = initSynchronizer();
        new Thread(
                () -> onHandleWork(intent)
            ).start();
        return binder;
    }

所以通过在另一个Thread中启动“onHandleWork”。这样你就可以使用:

“bindService(.....,JobIntentService.BIND_AUTO_CREATE);”

绑定到服务并返回您的Binder。无论如何当你从服务解除绑定时服务将被杀死,如果它仍然运行你不能再绑定到它,因为服务被杀死但是“onHandleWork”仍在运行的线程...

因此,我建议您仅在必须执行需要与活动进行通信的任务时使用此版本,直到它处于活动状态并且如果活动被杀死需要仍然工作(无法再次绑定jobService,但仅限于开始新的......)

要解除绑定后不要终止服务,你需要在“onDestroy”的“前景”“stopForeground”中启动它。这样,对于处理“onHandleWork”方法的线程,您的服务仍处于活动状态。

我希望google将解决这个快速的LoL,我将所有旧的“服务”和“IntentService”转换为新的工作但是......他们的工作比以前更糟糕!

再见有一个很好的编码;)


7
投票

我遇到了同样的问题(在预O设备上工作正常,没有任何关于O设备上发生任何事情的迹象)。今天,我再次尝试使用与昨​​天完全相同的代码,现在它可以工作 - 唯一的区别是我在两者之间重新启动了设备。

我目前的理论是我的初始设置不起作用;我当前的那个,只是重新部署新代码并没有清除JobScheduler中的破坏状态;重启或卸载/重新安装包。

现在正在运行的设置(从以前的IntentService迁移):

<service
    android:name=".MyJobIntentService"
    android:exported="false"
    android:permission="android.permission.BIND_JOB_SERVICE"/>

并开始

Intent intent = new Intent(); 
intent.putExtra(EXTRA_NAME, extraValue);
JobIntentService.enqueueWork(context, MyJobIntentService.class, FIXED_JOB_ID, intent);

请注意,intent不是显式intent(即,未设置ComponentName)。


2
投票

我遇到了这个问题,试图使用JobIntentServiceJobScheduler入队。虽然JobScheduler有自己的enqueueWork()方法,但它不适用于JobIntentService。该服务将启动,但永远不会调用onHandleWork()。

当我使用JobIntentService上的静态enqueueWork()方法时,它又开始工作了 - 例如:

MyJobIntentService.enqueueWork(context, ...)

通过阅读Android的javadoc,这一切都不明显。


2
投票

这对我有用,

按照@agirardello的建议删除IBind覆盖

并添加以下内容

    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

不知道为什么会这样。


2
投票

如果你在onCreate中覆盖了JobIntentService方法,它将阻止调用onHandleWork。

我将我的Service转换为JobIntentService,只有在我删除了onCreate方法之后才有效。


1
投票

我遇到了一个类似的问题,onHandleWork在从Service迁移到JobIntentService后第二次没有被调用。日志显示enqueueWork被调用但是onHandleWork只执行了第一次并且似乎被卡住了。

在进行了一些挖掘和测井之后,我发现不同的是,在“卡住”的情况下,即使在JobIntentService#onDestroy的所有操作都已执行并且看似已完成,但仍有onHandleWork

原来,罪魁祸首是bindService将该服务称为活动生命周期,这阻止了第一份工作的处理,并且出于某种原因在这种情况下调用enqueueWork导致服务“卡住”并且再也没有运行以下任何onHandleWork

所以,这是一个不正确的事件记录,其中JobIntentService在第一次调用后再次触发onHandleWork时似乎被卡住了:

enqueueWork -> first call
onHandleWork started (log in the first line)
onHandleWork finished (log in the last line)
enqueueWork -> second call
enqueueWork -> third call

以下是删除JobIntentService调用后bindService正常运行的事件的正确日志:

enqueueWork -> first call
onHandleWork started (log in the first line)
onHandleWork finished (log in the last line)
onDestroy (service is destroyed after the job is finished)
enqueueWork -> second call
onHandleWork started (log in the first line)
onHandleWork finished (log in the last line)
onDestroy
enqueueWork -> third call
onHandleWork started (log in the first line)
onHandleWork finished (log in the last line)
onDestroy

希望这会对某人有所帮助。


0
投票

对我来说,我仍然在enqueueWork之后开始服务,并因此而给我错误。


0
投票

只需尝试退出并再次运行Android Studio。然后再测试一下。在我的例子中,Android Studio的版本是v 3.3.1。请参阅正常工作的示例代码。

public class CustomizedIntentService extends JobIntentService
{
    public static final String MY_ACTION = "action.SOME_ACTION";
    private static final int MY_JOB_INTENT_SERVICE_ID = 500;

    public CustomizedIntentService() {
    }

    // Helper Methods to start this JobIntentService.
    public static void enqueueJobAction(Context context, String action) {
        Intent intent = new Intent(context, CustomizedIntentService.class);
        intent.setAction(MY_ACTION);

        enqueueWork(context, CustomizedIntentService.class, MY_JOB_INTENT_SERVICE_ID, intent);
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        String action = intent.getAction();

        // action will be "action.SOME_ACTION"
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onStopCurrentWork() {
        return super.onStopCurrentWork();
    }
}

//根据需要启动JobIntentService。

CustomizedIntentService.enqueueJobAction(context,CustomizedIntentService.MY_ACTION);


0
投票

听起来很有趣,我有一个类似的问题,因为我没有在enqueueWork()中将类的名称更改为自己的名称,因为我从我的一个类中复制了代码。在我进行更新后,它开始正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.