0

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.

Guven
  • 2,280
  • 2
  • 20
  • 34

1 Answers1

2

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+.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Very clear, thanks @CommonsWare. One last question: Any pointers on AppWidgetManager? Would it be possible to update my widget from within my app? – Guven Jul 22 '17 at 14:39
  • Also, any pointers on 'starting a service' would be great as well. Just a class name or documentation would be enough. – Guven Jul 22 '17 at 14:40
  • 1
    @Guven: "Would it be possible to update my widget from within my app?" -- yes. [Use `updateAppWidget()`](https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#updateAppWidget(int,%20android.widget.RemoteViews)). "any pointers on 'starting a service' would be great as well" -- `Service` is covered in [the documentation](https://developer.android.com/reference/android/app/Service.html) and in any decent book on Android app development. – CommonsWare Jul 22 '17 at 14:50