在小部件中使用按钮时应该使用活动还是服务?

发布于 2024-09-13 17:10:12 字数 347 浏览 6 评论 0原文

我已经尝试解决这个问题有一段时间了,但我却变得越来越困惑。

我制作了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一瞬间的火花 2024-09-20 17:10:12

我认为这根本不是基本的 - 这是我思考了一段时间与我编写的 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文