简体   繁体   English

AppWidgetProvider-跟踪小部件中的用户位置

[英]AppWidgetProvider - Tracking User Location in a Widget

I am just starting to understand how the Android widget lifecycle works and it seems that it is pretty much event-driven (broadcast driven). 我才刚刚开始了解Android小部件生命周期的工作原理,并且似乎它是由事件驱动(广播驱动)的。 There is no real persistent session; 没有真正的持续会话; your widget is woken up during the update and you are expected to update the views of your widget. 您的窗口小部件在更新过程中被唤醒,您应该更新窗口小部件的视图。

I can guess the answer to this question but can I track user location in a widget? 我可以猜出这个问题的答案,但是我可以在小部件中跟踪用户位置吗? If I understood the lifecycle right, the callbacks that I register with the LocationRequest class won't work for long since the class that I have used will be garbage collected. 如果我理解正确的生命周期,那么我在LocationRequest类中注册的回调将无法使用很长时间,因为我使用的类将被垃圾回收。 My only option is to get the location once during the update. 我唯一的选择是在更新过程中获取一次位置。

The framework allows a maximum (or minimum) of 30 minute update intervals. 该框架允许最大(或最小)30分钟的更新间隔。 Does that mean that the users will have to wait 30 minutes before they can see an updated location? 这是否意味着用户将不得不等待30分钟才能看到更新的位置? Are there any workarounds? 有什么解决方法吗?

Thanks. 谢谢。

your widget is woken up during the update and you are expected to update the views of your widget 您的窗口小部件在更新过程中被唤醒,您应该更新窗口小部件的视图

You are welcome to update the app widget's RemoteViews whenever you want, using AppWidgetManager . 欢迎您随时使用AppWidgetManager更新应用程序小部件的RemoteViews The update mechanism is simply a way to arrange to update it periodically, if desired. 如果需要,更新机制只是安排定期更新的一种方式。

can I track user location in a widget? 如何在小部件中跟踪用户位置?

Not directly. 不直接。 An app widget is a RemoteViews ; 一个应用程序小部件是RemoteViews it has no logic. 它没有逻辑。 An AppWidgetProvider is a subclass of BroadcastReceiver , and you cannot readily perform asynchronous, potentially-long operations in there. AppWidgetProviderBroadcastReceiver的子类,您不能在其中轻松执行异步的,可能很长的操作。

My only option is to get the location once during the update. 我唯一的选择是在更新过程中获取一次位置。

No. Your only reliable option is to start a service and have it get the location data, updating the app widget as needed. 否。唯一可靠的选择是启动服务,并让其获取位置数据,并根据需要更新应用程序小部件。

Note that continuously monitoring the location is a drain on the battery, which is one of the reasons why background location updates are curtailed on Android 8.0+. 请注意,持续监视位置会消耗大量电池,这是在Android 8.0+上限制后台位置更新的原因之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM