views:

32

answers:

1

I'm been trying to figure this out for a while now, and have just become more and more confused.

I have made a Android Widget which displays two articles (title + image). In addition to this, I have buttons for flipping backward and forward through the articles. What I don't understand is how I can change the Widgets RemoteViews when the buttons are pressed. Which should be one of the most basic operations in a widget, however, I can't seem to figure it out.

So...

  • Can I do this with just a OnClickListener in the AppWidgetProvider?

  • Or do I have to create an Activity without a window (visibility = false)?

Please excuse my stupidity. This is probably very basic.

A: 

I don't think this is basic at all - it's something I thought about for a while in relation to the Headset Blocker app I wrote, which is just a widget that you toggle on/off.

I ended up looking through Google's source code for what they did. The answer is to use the AppWidget's receiver nature to receive updates via setOnClickPendingIntent(). Then in onReceive(), you react differently to your own clicks than someone trying to create a widget. You can see a sample of what I did in the Headset Blocker source.

Ultimately, an Activity or a Service is too heavy weight for what you want. Using the same BroadcastReceiver as the app widget itself is much better.

Daniel Lew
"Ultimately, an Activity or a Service is too heavy weight for what you want." -- that depends on what you're doing. Bear in mind that `onReceive()` of a `BroadcastReceiver` is called on the main application thread. If you are going to do significant work on that thread (more than, say, 300ms), you should seriously consider delegating that work to an `IntentService`, so that work can be done with background priority on a background thread.
CommonsWare
True enough. I guess it depends on whether the title + image are retrieved when the user clicks the widget or if it's cached ahead of time.
Daniel Lew