[英]onDataSetChanged() on RemoteViewsFactory not being called after updating AppWidget
I have the following widget provider info: 我有以下窗口小部件提供商信息:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_agenda"
android:minHeight="110dp"
android:minResizeHeight="110dp"
android:minResizeWidth="110dp"
android:minWidth="180dp"
android:previewImage="@drawable/widget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="600000" android:widgetCategory="home_screen"/>
Used in the widget provider registered in the Manifest: 在清单中注册的窗口小部件提供程序中使用:
<receiver
android:name=".AgendaWidgetProvider"
android:label="MyWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/agenda_widget_info" />
</receiver>
My RemoteViewsService
is registered as well: 我的
RemoteViewsService
也已注册:
<service
android:name=".AgendaWidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
It has the following definition: 它具有以下定义:
class AgendaWidgetService : RemoteViewsService() {
override fun onGetViewFactory(intent: Intent?) =
AgendaWidgetViewsFactory(applicationContext)
}
To force update the widget and call the onUpdate()
method on AgendaWidgetProvider
I send the following broadcast: 要强制更新小部件并在
AgendaWidgetProvider
上调用onUpdate()
方法,我发送以下广播:
val widgetIds = AppWidgetManager.getInstance(context).getAppWidgetIds(
ComponentName(context, AgendaWidgetProvider::class.java)
)
if (widgetIds.isNotEmpty()) {
Intent(context, AgendaWidgetProvider::class.java).apply {
this.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds)
context.sendBroadcast(this)
}
}
This gets called every time! 每次都会调用它! My
onUpdate()
looks like this: 我的
onUpdate()
看起来像这样:
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
appWidgetIds.forEach {
val rv = RemoteViews(context.packageName, R.layout.widget_agenda)
rv.setTextViewText(R.id.widgetDate, "Today")
val i = Intent(context, AgendaWidgetService::class.java)
i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId)
rv.setRemoteAdapter(
R.id.widgetAgendaList,
i
)
rv.setEmptyView(R.id.widgetAgendaList, R.id.widgetAgendaEmpty)
appWidgetManager.notifyAppWidgetViewDataChanged(it, R.id.widgetDate)
appWidgetManager.notifyAppWidgetViewDataChanged(it, R.id.widgetAgendaList)
appWidgetManager.updateAppWidget(it, rv)
}
super.onUpdate(context, appWidgetManager, appWidgetIds)
}
The onDataSetChanged()
has the following def: onDataSetChanged()
具有以下定义:
override fun onDataSetChanged() {
Timber.d("AAA onDataSetChanged")
}
I use Android emulator (8.0) and Nexus 5x (8.1) to test this. 我使用Android模拟器(8.0)和Nexus 5x(8.1)进行了测试。 The
onUpdate()
is called every time, while onDataSetChanged()
seems to be called only when exiting or entering the app. 每次都会调用
onUpdate()
,而似乎仅在退出或进入应用程序时才调用onDataSetChanged()
。 Any clues? 有什么线索吗?
Thanks! 谢谢!
To trigger onDataSetChanged()
in a widget, you don't need to create a broadcast. 要在窗口小部件中触发
onDataSetChanged()
,您无需创建广播。 Just call notifyAppWidgetViewDataChanged
from within your app, like this: 只需从您的应用程序内调用
notifyAppWidgetViewDataChanged
,如下所示:
Context context = getApplicationContext();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, AgendaWidgetProvider.class));
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widgetAgendaList);
Also, you shouldn't need to call notifyAppWidgetViewDataChanged()
in onCreate()
. 另外,您不需要在
onCreate()
调用notifyAppWidgetViewDataChanged()
onCreate()
。 According to this answer , onCreate()
automatically triggers onDataSetChanged()
. 根据此答案 ,
onCreate()
自动触发onDataSetChanged()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.