Remove background location access on Android
Asked Answered
D

1

5

I am using react-native-geolocation-service to fetch the location of the user. On iOS it is easy to just ask for location access when the app is in the foreground, you just skip adding background location access as a capability. However, I don't understand how you remove this permission on Android. I don't have the ACCESS_BACKGROUND_LOCATION permission set and I'm still getting the option "Allow all the time" on e.g. my Pixel 2.

This is the code I am using to fetch the location of the user:

const getCurrentPosition = async (onSuccess, onError) => {
  if (Platform.OS === 'android') {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      {
        title: 'My title',
        message: 'My message',
        buttonPositive: 'Continue',
      }
    );
    if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
      onError();
      return;
    }
  }
  Geolocation.getCurrentPosition(
    (result) => {
      const position = {
        longitude: result.coords.longitude,
        latitude: result.coords.latitude,
      };
      onSuccess(position);
    },
    (error) => {
      onError();
    },
    { enableHighAccuracy: true, maximumAge: 10000, timeout: 15000 }
  );
};
Darcidarcia answered 26/10, 2020 at 8:49 Comment(2)
Daniel, have you resolved this issue? I have a same problem myself.Roguish
Unfortunately not :/Darcidarcia
R
7

Look into your main AndroidManifest.xml file (and by main I mean under android/app/src/main(COULD_BE_SOMETHING_ELSE_USUALLY_MAIN_BUT_YMMV)/AndroidManifest.xml)

You need to find a bunch of <uses-permission />. Check if one of these bad boys got ACCESS_BACKGROUND_LOCATION:

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

No luck? Fear not, my dear friend. Here's a remedy for your battle-fatigued-from-dealings-with-react-native mind. Add this line under the last <uses-permission> tag.

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"
                     tools:node="remove" />

May peace prevail.

Technical nitty gritty:

Actually, a library, like react-native-geolocation-service or react-native-background-geolocation, can alter final AndroidManifest.xml, which will go to a bundle edited heavily.

Simply deleting a line won't cut it, because you don't have an access to that final draft, cause it's auto-generated. You need to be one step ahead and use tools:node="remove" to make sure it will be deleted from the final version of the manifest file.

Sources:

Roguish answered 19/1, 2021 at 13:45 Comment(5)
You may need to add xmlns:tools="schemas.android.com/tools" to the manifest headerNellnella
Thx. Can you elaborate or provide a link on why one should do it?Roguish
The reason for adding the "remove" instruction is to drop this permission from your app, even though some of your dependencies/libraries sometimes use it. For example when there is a feature in the dependency that requires this permission, but you don't use that feature. Removing it means your app won't require this permission and thus won't have to confirm to Google's requirements for apps that do use background location permission.Filtrate
@PerChristianHenden thx for reply! I'd asked particularly about xmlns:tools="schemas.android.com/tools" bit in @Nellnella comment. Maybe, you could answer this?Roguish
Yes, the xmlns:tools statement is to import the tools namespace, so that tools:node is available for the later statement.Filtrate

© 2022 - 2024 — McMap. All rights reserved.