How can I update the UI for an android appwidget

2019-05-23 00:37发布

问题:

I'm sure I'm missing something, but I'm just trying to get an app widget with a button and a counter. Every time I click the button I want the counter to update by 1.

I've set the onUpdate() function of the WidgetProvider to register a pending event to the button so that it starts a service to bump the counter numbver:

Intent active = new Intent(context, CounterService.class);
active.setAction(CounterService.COUNT);
PendingIntent pending = PendingIntent.getService(context, 0, active, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.CountButton, pending);

ComponentName component = new ComponentName(context.getPackageName(), KickCounterWidgetProvider.class.getName());    
appWidgetManager.updateAppWidget(component, views);

Then in the services CounterService::onStart() function I bump the count (stored in prefs for now) and then try and update the text field that shows the current count value:

// ... bump the count here and store a string representation of it in currentCountString ...

RemoteViews remoteView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.widget);

remoteView.setTextViewText(R.id.CurrentKickCount, currentCountString);

// apply changes to widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName component = new ComponentName(getApplicationContext().getPackageName(), KickCounterWidgetProvider.class.getName());    
appWidgetManager.updateAppWidget(component, remoteView);

Judicious use of Logcat indicates tha this all works ok and the string seems to be OK, but for some reason the call to appWidgetManager.updateAppWidget() seems to be failing silently for some reason.

I don't know if it's related at all, but the first time I add an instance of the widget to the homescreen, not even the button works (ie the call to updateAppWidget() in onUpdate() in the Provider fails). Subsequent instances of the widget seem to work OK for the call to updateAppWidget() in the Provider but never works for the service.

Any help would be greatly appreciated.

回答1:

From: http://code.google.com/p/android/issues/detail?id=8889

When installing a widget onto a freshly wiped or newly created AVD, appWidgetManager.updateAppWidget calls are not updating the respective
widgets. Intents are properly received, the calls are called normally, yet no widget updates occur.

If you restart the AVD (with or without the widget's package installed on the device) the problem cease to exist after the first non freshly initialized boot.

The issue seems to exist on 2.0 and 2.1, on 1.5, 1.6 and 2.2 it is behaving as expected.



回答2:

Your general approach seems sound, though this probably works as well as getApplicationContext() here. Your last full paragraph suggests that something else may be awry, though. Here is a somewhat complicated sample project that demonstrates the sort of pattern you're using -- in this case, randomly choosing a restaurant rather than bumping a counter.