Friday 13 November 2020

Detect location for Android AppWidget without background location permission

I have an app for providing a weather forecast for a chosen location, which includes a widget. As an option, the user can choose for the app and widget to show weather for the device's location, which is clearly a useful feature for the user.

For this, location permission is required (at least ACCESS_COARSE_LOCATION). And for the widget, because it (optionally) updates every e.g. hour in the background, without an app open in the foreground, this requires background location permission to be granted (ACCESS_BACKGROUND_LOCATION) in order to get the latest device location at the time the widget updates.

But because of a tightening of policy taking effect, updates to my app are now being rejected by Google. Apparently I have to remove the background location permission. Which is a shame because it is (a) clearly a useful feature from a user perspective; (b) entirely optional... the user does not have to grant it if they do not want to... the user is not forced into it and there is an explanation given as to why background location permission is required for a widget... the app itself is still usable (with detected location) without granting background location permission.

Is there any way to get the device's location for use in a widget, without the ACCESS_BACKGROUND_LOCATION permission? Currently I am using the following to get the location with the FusedLocationProviderClient :

FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
mFusedLocationClient.getLastLocation().addOnCompleteListener(new LocationOnCompleteListener(context, listener, appWidgetId));


from Detect location for Android AppWidget without background location permission

No comments:

Post a Comment