Android 10 (Q) ACCESS_BACKGROUND_LOCATION permission

情到浓时终转凉″ 提交于 2019-12-23 04:55:57

问题


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:


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.



来源:https://stackoverflow.com/questions/58816066/android-10-q-access-background-location-permission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!