开发者

Should one use an Activity or a Service when using a button in a Widget?

I'm be开发者_JAVA技巧en 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜