【问题标题】:Home widget not refreshing on Oreo using JobIntentService使用 JobIntentService 在 Oreo 上不刷新主页小部件
【发布时间】:2018-06-19 07:30:20
【问题描述】:

我的应用包含使用IntentService 定期更新的主页小部件。

现在,当面向 API 级别 26 时,由于 Oreo 的限制,我不得不稍作更改。您可以找到更多信息here

我找到了this article,其中描述了如何使 IntentService 在 Oreo 上工作。

我按照描述做了所有事情:

  • 将我的IntentService 转换为JobIntentService
  • 将所有逻辑从onHandleIntent() 移至onHandleWork()
  • 在 AndroidManifest 文件中的服务中添加了android:permission="android.permission.BIND_JOB_SERVICE"
  • 另外,<uses-permission android:name=”android.permission.WAKE_LOCK” /> 已在此项目中使用
  • 我更进一步,开始使用以下逻辑来启动服务:enqueueWork(context, LocationForecastService.class, JOB_ID, work);

它仍然没有正确刷新或初始化小部件。服务实际启动,下载数据,但问题可能出在通过广播发送数据的某个地方。当 API 级别设置为 24 时有效。

小部件在 AndroidManifest 文件中注册为 BroadcastReceivers,如下所示:

<receiver
        android:name=".widgets.provider.WeekWidgetProvider"
        android:label="@string/Widget_WeekForecast_Title">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="action_widget_data_change_event" />
            <action android:name="action_week_widget_data_change_event" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/appwidget_week_provider_layout" />
</receiver>

有人遇到过类似的问题吗?

【问题讨论】:

  • 显示你向接收者发送广播的代码
  • 你搞定了吗?另外,正如@HeyAlex 所说,您没有向我们展示如何从JobIntentService 返回数据的代码。

标签: android android-widget android-intentservice jobintentservice


【解决方案1】:

为了完成这项工作,我必须按照上面问题中提供的 URL 中的描述实现所有内容。

在制作 Intent 发送广播时要小心。

代替

Intent intent = new Intent(action);

应使用以下代码:

Intent intent = new Intent(context, clazz);
intent.setAction(action);

clazz 是小部件提供者类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-30
    • 2021-01-15
    • 2020-09-17
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    相关资源
    最近更新 更多