Android Oreo+:AppWidget 点击不调用 JobIntentService.onHandleWork()

Android Oreo+ : AppWidget click doesn't invoke JobIntentService.onHandleWork()

我有一个小部件。完成 SettingsActivity 从 Internet 加载数据并将其显示在 ListView 中。这适用于奥利奥之前和奥利奥设备。

但以下两种行为不会由任何 activity 触发,而是通过点击小部件本身(= 背景)触发,并且仅适用于奥利奥之前的设备:

但是在遇到像

这样的异常之后,在 Oreo 上
Not allowed to start service Intent

我从 IntentService 切换到 JobIntentService 是因为:

From developer.android.com :

IntentService is subject to these restrictions IntentService is a service, and is therefore subject to the new restrictions on background services. As a result, many apps that rely on IntentService do not work properly when targeting Android 8.0 or higher. For this reason, Android Support Library 26.0.0 introduces a new JobIntentService class, which provides the same functionality as IntentService but uses jobs instead of services when running on Android 8.0 or higher.

清单:

<service
            android:name="com.example.MyWidgetService"
            android:permission="android.permission.BIND_JOB_SERVICE"/>

服务:

public class MyWidgetService extends JobIntentService {
    @Override
    protected void onHandleWork(Intent intent) {
        XLog.i("onHandleWork"); <-- never printed to log
    }
}

在 Widget 的 RemoteViewsService 中:

remote.setOnClickPendingIntent(R.id.open, new Intent(..some-code..));

修改后的代码适用于 Oreo 之前的设备,但在 Oreo 上既不点击列表视图项目也不点击按钮什么都不做,MyWidgetService.onHandleWork() 永远不会被调用。

知道我做错了什么吗?

JobIntentService 不是处理带有 PendingIntent 的点击的方法,因为它不会很快触发它(使用 JobIntentService 它可以接收一分钟或更长时间的回调...)。所以我只是用 startForegroundService 开始服务,因为它保证在用户单击您的 RemoteView 时调用您的服务。

P.S。是的,您需要显示通知(例如 AppNameWidget 更新...),但它在 oreo 上完美运行。