自定义 JobIntentService 的 onHandleWork 方法未被调用

37

我最近更新了一款应用程序,使用JobIntentService处理来自推送的通知,而不是常规的IntentService,因为这似乎是在 Lollipop设备之前和之后正确处理此问题的方法。我正在这样排队工作:

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中。 有成功集成它的人可以帮忙吗?

更新1

我在一个API级别为21的设备上测试了这个,它可以工作...但似乎在我的Android Oreo Pixel XL设备上没有被调用...有什么线索吗?

更新2

另外,我似乎看到IntentService的onCreate被调用,但没有其他生命周期方法(包括onHandleWork)。还有人遇到过这种情况吗?


1
我有相同的症状,但还没有答案。在API级别23设备上运行良好,在API级别26 Nexus 5X上不行。你的“work” Intent是如何构建的? - Jule
@Jule 目前我的工作意图是使用自定义操作加上一些额外的内容来构建它,就这样。 - Eshaan
1
我遇到了相同的问题,实际上我收到了这个消息:“JobServiceContext: 在尝试绑定时超时...” - agirardello
12个回答

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);


-2
对于那些无法通过其他答案解决问题的人:
尝试在每次调用enqueueWork时使用不同的JOB_IDs。如果之前的作业没有完成,服务可能会被卡住(类似于用户“git pull origin”所描述的问题),使用不同的ID可能会解决此问题。

1
这会导致 java.lang.IllegalArgumentException: 给定的作业ID 1001与之前的1000不同 - Chisko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接