Reputation: 2310
I am just starting to understand how the Android widget lifecycle works and it seems that it is pretty much event-driven (broadcast driven). 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. My only option is to get the location once during the update.
The framework allows a maximum (or minimum) of 30 minute update intervals. Does that mean that the users will have to wait 30 minutes before they can see an updated location? Are there any workarounds?
Thanks.
Upvotes: 0
Views: 780
Reputation: 1006614
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
. 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
; it has no logic. An AppWidgetProvider
is a subclass of BroadcastReceiver
, and you cannot readily perform asynchronous, potentially-long operations in there.
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+.
Upvotes: 2