10

I respond to a button press on my appwidget in the onreceive method. When the button I pressed, I want to force the widget to call the onupdate method. How do I accomplish this?

Thanks in advance!

strange quark
  • 5,205
  • 7
  • 41
  • 53
  • I THINK it's this command: appWidgetManager.updateAppWidget(appWidgetId, views); Anyway, how do you get it to respond to someone clicking on it? I asked that in a question here: http://stackoverflow.com/questions/2748590/clickable-widgets-in-android – Leif Andersen May 01 '10 at 02:20
  • see http://stackoverflow.com/questions/2748590/clickable-widgets-in-android/2748759#2748759 – larham1 Nov 21 '13 at 01:15

2 Answers2

9

Widget can't actually respond to clicks because it's not a separate process running. But it can start service to process your command:

public class TestWidget extends AppWidgetProvider {
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch UpdateService
            Intent intent = new Intent(context, UpdateService.class);
            PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

            // Get the layout for the App Widget and attach an on-click listener to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
            views.setOnClickPendingIntent(R.id.button, pendingIntent);

            // Tell the AppWidgetManager to perform an update on the current App Widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    public static class UpdateService extends Service {
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
          //process your click here
          return START_NOT_STICKY;
        }
    }
}

You should also register the new service in your manifest file:

<service android:name="com.xxx.yyy.TestWidget$UpdateService">

You can find another example of UpdateService implementation in Wiktionary sample in SDK

And here's another good approach Clickable widgets in android

Community
  • 1
  • 1
Fedor
  • 43,261
  • 10
  • 79
  • 89
  • The "another good approach" link kinda contradicts your initial statement. Widgets can respond to clicks without needing a service involved by using the onReceive handler with an intent that gets sent when you click the widget item. Not having a service involved is a better solution in my opinion. – jt-gilkeson Sep 25 '12 at 01:21
  • onStart in now DEPRECATED – Stefano Munarini Oct 29 '13 at 09:54
3

This is kinda crude, but it works rather well for me, as I've found no directly-implemented way to force an update.

public class Foo extends AppWidgetManager {
   public static Foo Widget = null;
   public static Context context;
   public static AppWidgetManager AWM;
   public static int IDs[];

   public void onUpdate(Context context, AppWidgetManager AWM, int IDs[]) {
      if (null == context) context = Foo.context;
      if (null == AWM) AWM = Foo.AWM;
      if (null == IDs) IDs = Foo.IDs;

      Foo.Widget = this;
      Foo.context = context;
      Foo.AWM = AWM;
      Foo.IDs = IDs;
.......
   }
}

Now, anywhere I want to force the widget to update, it's as simple as:

if (null != Foo.Widget) Foo.Widget.onUpdate(null, null, null);
Izkata
  • 8,961
  • 2
  • 40
  • 50