9

I need the user to check and set permission to ACCESS_BACKGROUND_LOCATION ("Allow all the time") without the option to "Allow only while using the app". My GPS Tracking app needs to continue accessing location even when the application is minimised.

I have already permissions working for ACCESS_FINE_LOCATION for Android 9.

AndroidManifest.xml--------------------------------------------------->

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
TrackingActivity.java (need to include ACCESS_BACKGROUND_LOCATION)---->

private void permissionCheck1() {
        int permissionCheck1 = ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION);
        if (permissionCheck1 != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
        } else {
            grantedSetupGPSandMap();
        }
}

I need the only permission option to be "Allow all the time" if possible. How do I set this up when checking and setting permissions?

CodeWithVikas
  • 1,105
  • 9
  • 33
Chris King
  • 225
  • 1
  • 4
  • 14

1 Answers1

23

For Android 10, if:

  • You omit ACCESS_BACKGROUND_LOCATION, and:
  • You use a foreground service with the android:foregroundServiceType="location" property set in the manifest, then:

The system will supply only the "Allow only while using the app" and "Deny" options to the user, in reference to the location permission.

I realize that's the exact opposite of what you asked for, but the point is, under this condition, "Allow only while using the app" effectively behaves like "Allow all the time", provided the aforementioned foreground service is running.

You will be able to receive location updates even when the app is offscreen. And the user does not have the option of only partially allowing location updates; they have to choose between fully allowing it, or fully denying it.

EDIT

This answer also assumes your targetSdkVersion is 29.

greeble31
  • 4,894
  • 2
  • 16
  • 30
  • 2
    Can't get the "Allow all the time" prompt... SDK is 29, permissions are set in the manifest and only return the "When app is open" and "Deny" choices. This is driving me crazy – Warface May 04 '20 at 12:20
  • DId you get Allow all the time permission? i am also facing this issue. – Sardar Khan Jun 20 '20 at 10:58
  • @SardarKhan As this answer attempts to explain, you're not supposed to get the "allow all the time" prompt. – greeble31 Jun 20 '20 at 13:17
  • @greeble31 i am passing accessbackground location permission but still no allow all the time option. – Sardar Khan Jun 26 '20 at 22:43
  • Hmm I totally thought this answer was correct. I had my app set up like this as well, but for some reason, the app does not process BLE when it's off screen with this setup... =X Everything works fine when the app is on screen, but the second it's not, BLE stops working. – Thomas Stubbe Jan 22 '21 at 13:54
  • @ThomasStubbe I think this is more for the on-board GPS subsystem; doesn't really have any effect on BLE. – greeble31 Jan 23 '21 at 13:45