小部件活动
我有一个问题,我有一个小部件需要使用活动发出 http 请求并从互联网获取一些更新。我的问题是小部件仅使用活动进行更新。我放置了一个按钮来启动活动,但这不是实际上是解决方案。我需要该活动在小部件工作时自行启动并定期终止。我怎样才能实现这一点?
I have a problem that I have a widget that needs to make a http request using an activity and get some updates from internet.My problem is widget is doing the updates only using the activity.I put a button to start the activity but that's not the solution actually.I need that activity to start himself and terminate periodically when the widget is working.How can I achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您正在谈论应用程序小部件,我认为您对小部件的想法是不正确的;小部件基本上只是一个视图,仅此而已,因此它无法从任何东西请求数据。如果您想更新小部件呈现的数据,您可以使用服务来执行 httprequest,然后通过广播您自己的操作意图并在 AppWidgetProvider 的 onRecieve 中处理该意图或直接更新小部件,将其提供给小部件。
当然,如果您的活动处于活动状态,它也可以更新小部件,并且您可以通过在 AlarmManager 中设置重复警报来启动活动,但如果您在执行 HttpRequest 时不必显示任何内容,我建议不要使用活动。
I think your idea of a widget is not right assuming you are talking about app widgets; a widget is basically just a view and nothing more, so it cannot request data from anything. If you want to update the data presented by the widget you can use a service to do the httprequest, and then serve it to the widget either by broadcasting your own action intent and handling that in the onRecieve of your AppWidgetProvider or update the widget directly.
Ofcourse if your activity is active it can update the widget as well, and you can start the activity by setting a repeating alarm in the AlarmManager, but if you don't have to display anything whilst doing the HttpRequest i would recommend against using an activity.
看看是否有帮助:
http://developer.android.com/resources/articles/timed-ui -updates.html
See if it helps:
http://developer.android.com/resources/articles/timed-ui-updates.html