为什么我手动更新的 App Widgets 没有更新?
Why are my manually updated AppWidgets not updating?
我有两个小部件:一个叫做“NoteWidget”,是一个普通的应用程序小部件,具有配置Activity。每当我第一次设置它时,它都会调用 onUpdate(),但它再也不会调用了。
另一个是“UpcomingWidget”,这个是 Collection Widget,但是因为这个没有 Configuration Activity,onUpdate() 永远不会被调用。
这很奇怪,因为它曾经在我只有 NoteWidget 时工作,甚至在我开始实现 UpcomingWidget 时,但在某个时候它停止工作了。
我使用 refreshWidgets() 方法手动更新所有小部件。它确实被调用,但 onUpdate() 不在任何小部件提供程序中 class:
fun refreshWidgets(context: Context) {
val manager = AppWidgetManager.getInstance(context)
val noteIntent = Intent(context, NoteWidget::class.java)
val upcomingIntent = Intent(context, UpcomingWidget::class.java)
noteIntent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
upcomingIntent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
val noteIds = manager.getAppWidgetIds(ComponentName(context, NoteWidget::class.java))
val upcomingIds = manager.getAppWidgetIds(ComponentName(context, UpcomingWidget::class.java))
noteIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, noteIds)
upcomingIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, upcomingIds)
context.sendBroadcast(noteIntent)
context.sendBroadcast(upcomingIntent)
}
AndroidManifest.xml
<receiver android:name=".widgets.NoteWidget" android:label="@string/note_widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/note_widget_info" />
</receiver>
<receiver android:name=".widgets.UpcomingWidget" android:label="@string/upcoming_widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/upcoming_widget_info" />
</receiver>
<service
android:name=".widgets.UpcomingWidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
<activity android:name=".activities.NotePicker">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
note_widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/note_widget"
android:initialLayout="@layout/note_widget"
android:minWidth="110dp"
android:minHeight="110dp"
android:minResizeHeight="110dp"
android:minResizeWidth="110dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="0"
android:widgetCategory="home_screen"
android:configure="com.byteseb.grafobook.activities.NotePicker"/>
upcoming_widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/upcoming_widget"
android:initialLayout="@layout/upcoming_widget"
android:minWidth="110dp"
android:minHeight="110dp"
android:minResizeHeight="110dp"
android:minResizeWidth="110dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="0"
android:widgetCategory="home_screen" />
正如@MikeM 在评论中提到的,问题是我传递了 AppWidgetManager.EXTRA_APPWIDGET_ID 作为额外的名称,而它应该是 AppWidgetManager.EXTRA_APPWIDGET_IDS.
我有两个小部件:一个叫做“NoteWidget”,是一个普通的应用程序小部件,具有配置Activity。每当我第一次设置它时,它都会调用 onUpdate(),但它再也不会调用了。
另一个是“UpcomingWidget”,这个是 Collection Widget,但是因为这个没有 Configuration Activity,onUpdate() 永远不会被调用。
这很奇怪,因为它曾经在我只有 NoteWidget 时工作,甚至在我开始实现 UpcomingWidget 时,但在某个时候它停止工作了。
我使用 refreshWidgets() 方法手动更新所有小部件。它确实被调用,但 onUpdate() 不在任何小部件提供程序中 class:
fun refreshWidgets(context: Context) {
val manager = AppWidgetManager.getInstance(context)
val noteIntent = Intent(context, NoteWidget::class.java)
val upcomingIntent = Intent(context, UpcomingWidget::class.java)
noteIntent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
upcomingIntent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
val noteIds = manager.getAppWidgetIds(ComponentName(context, NoteWidget::class.java))
val upcomingIds = manager.getAppWidgetIds(ComponentName(context, UpcomingWidget::class.java))
noteIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, noteIds)
upcomingIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, upcomingIds)
context.sendBroadcast(noteIntent)
context.sendBroadcast(upcomingIntent)
}
AndroidManifest.xml
<receiver android:name=".widgets.NoteWidget" android:label="@string/note_widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/note_widget_info" />
</receiver>
<receiver android:name=".widgets.UpcomingWidget" android:label="@string/upcoming_widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/upcoming_widget_info" />
</receiver>
<service
android:name=".widgets.UpcomingWidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
<activity android:name=".activities.NotePicker">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
note_widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/note_widget"
android:initialLayout="@layout/note_widget"
android:minWidth="110dp"
android:minHeight="110dp"
android:minResizeHeight="110dp"
android:minResizeWidth="110dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="0"
android:widgetCategory="home_screen"
android:configure="com.byteseb.grafobook.activities.NotePicker"/>
upcoming_widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/upcoming_widget"
android:initialLayout="@layout/upcoming_widget"
android:minWidth="110dp"
android:minHeight="110dp"
android:minResizeHeight="110dp"
android:minResizeWidth="110dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="0"
android:widgetCategory="home_screen" />
正如@MikeM 在评论中提到的,问题是我传递了 AppWidgetManager.EXTRA_APPWIDGET_ID 作为额外的名称,而它应该是 AppWidgetManager.EXTRA_APPWIDGET_IDS.