Android 10 (Q) ACCESS_BACKGROUND_LOCATION permissi

2020-07-17 04:38发布

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?

1条回答
我想做一个坏孩纸
2楼-- · 2020-07-17 04:59

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.

查看更多
登录 后发表回答