The onUpdate method is executed only if the widget is initialized (for example, placed on the desktop) or updatePeriodMillis expires. If you want to perform a service, for example. by clicking on the widget, you should “attach” the pending intent as follows:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final Intent intent = new Intent(context, UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout....);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
for(int i=0,n=appWidgetIds.length;i<n;i++){
int appWidgetId = appWidgetIds[i];
appWidgetManager.updateAppWidget(appWidgetId , views);
}
(cleaned version of the working widget).
The fact is that the onUpdate () method is really very rarely executed. Actual interaction with widgets is set using pending intentions.
tichy source
share