在小部件中使用按钮时应该使用活动还是服务?
我已经尝试解决这个问题有一段时间了,但我却变得越来越困惑。
我制作了一个 Android Widget,它显示两篇文章(标题+图像)。除此之外,我还有用于向后和向前翻阅文章的按钮。我不明白的是,按下按钮时如何更改小部件 RemoteViews。这应该是小部件中最基本的操作之一,但是,我似乎无法弄清楚。
那么...
我可以仅使用 AppWidgetProvider 中的 OnClickListener 来完成此操作吗?
或者我是否必须创建一个没有窗口的活动(visibility = false)?
请原谅我的愚蠢。这可能是非常基本的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这根本不是基本的 - 这是我思考了一段时间与我编写的 Headset Blocker 应用程序相关的事情,它只是一个可以打开/关闭的小部件。
我最终查看了谷歌的源代码,了解他们做了什么。答案是使用 AppWidget 的接收器性质通过
setOnClickPendingIntent()
。然后,在onReceive()
中,您对自己的点击的反应与尝试创建小部件的人不同。 您可以请参阅我在 Headset Blocker 源代码中所做的示例。最终,活动或服务对于您想要的东西来说太重了。使用与应用程序小部件本身相同的 BroadcastReceiver 效果要好得多。
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 inonReceive()
, 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.