【发布时间】:2022-07-29 16:37:47
【问题描述】:
在我的 AppWidgetProvider 类中,onUpdate() 方法中有以下代码:
// Update each of the widgets with the remote adapter
for (int appWidgetId : appWidgetIds) {
HyperLog.i(TAG, "Updating Widget id " + appWidgetId);
...
HyperLog.i(TAG, "Starting updateAppWidget with id " + appWidgetId);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.lvActiveReminders);
appWidgetManager.updateAppWidget(appWidgetId, rv);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
但是调用notifyAppWidgetViewDataChanged()并没有调用RemoteViewService类方法onDataSetChanged():
HyperLog.i(TAG, "onDataSetChanged is called.");
getData();
有没有人暗示为什么不调用它?
始终调用 AppWidgetProvider 中的 onUpdate() 并且 updateAppWidget 刷新小部件,但使用旧数据,因为之前未调用 onDataSetChaned()。
在文档中,这是我们刷新小部件数据的方式 (https://developer.android.com/guide/topics/appwidgets/collections#keep-data-fresh)。
我正在 Android 12 设备上对此进行测试。
【问题讨论】: