简体   繁体   English

更新AppWidget后未调用RemoteViewsFactory上的onDataSetChanged()

[英]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.

相关问题 调用notifyAppWidgetViewDataChanged后,在RemoteViewsFactory中不调用onDataSetChanged - onDataSetChanged is not called in RemoteViewsFactory after notifyAppWidgetViewDataChanged is called 在AppWidget的RemoteViewsFactory中onCreated()之后依赖对onDataSetChanged()的调用是否安全? - Is it safe to rely on the call to onDataSetChanged() after onCreated() in RemoteViewsFactory of an AppWidget 在 notifyAppWidgetViewDataChanged 后未调用 RemoteViewService 中的 onDataSetChanged() - onDataSetChanged() in RemoteViewService is not called after notifyAppWidgetViewDataChanged 在onDataSetChanged之后如何将RemoteViewsService更新状态返回给appWidget? - How to return a RemoteViewsService update status to appWidget after onDataSetChanged? Android updateAppWidget 被调用但没有更新小部件或到达 RemoteViewsFactory? - Android updateAppWidget is called but not updating the widget or reaching RemoteViewsFactory? 在 RemoteViewsFactory 的 onDataSetChanged 中使用协程 runBlocking() 是否安全? - Is it safe to use Coroutines runBlocking() inside onDataSetChanged of RemoteViewsFactory? Android AppWidget RemoteViewsFactory意向和putSerializable / putParcelable - Android AppWidget RemoteViewsFactory intent and putSerializable/putParcelable Android Widgets:updateAppWidget()未调用onDataSetChanged - Android Widgets: onDataSetChanged not called by updateAppWidget() Android - 重启后不会更新Remoteview的Appwidget - Android - Appwidget with Remoteviews not updating after reboot 更新AppWidget - Updating AppWidget
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM