简体   繁体   中英

Android updateAppWidget doesn't update

I got a serious issue on my Android application :

Calling

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(appWidgetId, getWidgetView(context, appWidgetId));

in the public void onReceive(Context context, Intent intent) method has no result at all. The RemoteViews that are produced looks to be valid (programatically speaking) but the widget is not updated (It keeps old values).

I've found some similar questions, but nobody has answer it:

Solved:

The solution was quite weird but using

appWidgetManager.updateAppWidget(new ComponentName(context, CustomWidgetProvider.class), getWidgetView(context, appWidgetId));

solved my issue.

The solution posted by @Manitoba is not fully working, here is the working code to put inside onUpdate :

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
// do your stuff
appWidgetManager.updateAppWidget(new ComponentName(context, CustomWidgetProvider.class), views);

Hope this will help :)

What I've found for updating widget for xPeria is to add after .putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

This line :

<intent>.setData(Uri.withAppendedPath(
        Uri.parse("imgwidget://widget/id/"),
        String.valueOf(allWidgetIds)));

Where is

Intent active = new Intent(context, <widget_provider>.class);

Tried all updateAppWidget() overloads, but it just doesn't work - whether called from onReceive() or Activity . Strangely, notifyAppWidgetViewDataChanged() does work in the same scenario.

EDIT:

Android/Launcher must be caching RemoteViews , and if you call updateAppWidget() with exactly same RemoteViews - no rebind happens, despite what docs say, I can clearly see that my RemoteViewsFactory is not called. Because I needed to force UI rebind, ended up calling updateAppWidget() with some dummy layout, and immediately after that with normal layout again:

            try { 
                mgr.updateAppWidget(widgetId, new RemoteViews(context.getPackageName(), R.layout.dummy)); 
            } finally { 
                mgr.updateAppWidget(widgetId, GetRemoteView(context, widgetId)); 
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM