PermissionsDispatcher icon indicating copy to clipboard operation
PermissionsDispatcher copied to clipboard

ACCESS_BACKGROUND_LOCATION permission has issue when API less than Android Q

Open TankJK opened this issue 5 years ago • 12 comments

FAQs


Overview

ACCESS_BACKGROUND_LOCATION is not handled for Android API less than 29.

  • Describe the issue briefly

Compile and target SDK 29 From Android Q, special permission is required for accessing location in background. This did works well for Android Q but when its less than Android Q, even though user has given Allow permission for location, it consider failure (deny) for ACCESS_BACKGROUND_LOCATION

Expected

  • What is the expected behavior?

  • When Android API less than 29, when user has given Allow permission for location, it should consider success for ACCESS_BACKGROUND_LOCATION.

Actual

  • What is the actual behavior?

  • When Android API less than 29, when user has given Allow permission for location, it consider failure (deny) for ACCESS_BACKGROUND_LOCATION.

Environment

  • Which library version are you using? 4.5.0

  • On which devices do you observe the issue? All which has OS less than 29.

  • Note any other information that might be useful

Reproducible steps

  • While it's not required, it'd be perfect to add a link to a sample project where you encounter the issue

Please guide.

Thanking you in advance, TankJK

TankJK avatar Oct 25 '19 06:10 TankJK

@TankJK I'm afraid it's not related to our library. Have you tried below?

Allows an app to access location in the background. If you're requesting this permission, you must also request either ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION. Requesting this permission by itself doesn't give you location access.

https://developer.android.com/reference/android/Manifest.permission#ACCESS_BACKGROUND_LOCATION

hotchemi avatar Oct 25 '19 07:10 hotchemi

@hotchemi Yes all permission is been added.

TankJK avatar Oct 25 '19 09:10 TankJK

@TankJK really? I wonder because ACCESS_BACKGROUND_LOCATION is from API level 29?

hotchemi avatar Oct 25 '19 09:10 hotchemi

@hotchemi App all permission: ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION ACCESS_BACKGROUND_LOCATION

If it's API below 29, simply Allow should give all three permissions. While in 29, users have 2 choices:

  1. While using app (which is ACCESS_FINE_LOCATION)
  2. Allow all the time (which is ACCESS_FINE_LOCATION + ACCESS_BACKGROUND_LOCATION)

But problem occurs when API is less then 29 as described in issue.

TankJK avatar Oct 25 '19 09:10 TankJK

we'll take a look

hotchemi avatar Oct 28 '19 07:10 hotchemi

@TankJK thank you we could replicate the issue.

Actually the root cause is ACCESS_BACKGROUND_LOCATION doesn't exist but you request the permission to Android and it always return -1(DENIED). Kind of obvious but this can be an improvement point for our library, let us think a bit.

A quick workaround is like below on your side.

@NeedsPermission(Manifest.permission.ACCESS_FINE_LOCATION)
fun method1() = TODO

@RequiresApi(api = Build.VERSION_CODES.Q)
@NeedsPermission(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
fun method2() = TODO

fun test() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            method2WithPermissionCheck()
        } else {
            method1WithPermissionCheck()
        }
}

Ref:

  • https://developer.android.com/training/location/receive-location-updates
  • https://github.com/permissions-dispatcher/PermissionsDispatcher/compare/prototype/background_permission?expand=1

hotchemi avatar Oct 28 '19 15:10 hotchemi

I also encountered the same problem {Manifest.permission.ACTIVITY_RECOGNITION} This permission is added after api29 and cannot be run in lower versions Looking forward to your updates

lumina0o0 avatar Apr 02 '20 09:04 lumina0o0

问题。。回调了怎么办。 请求权限分成2个方法。回调的时候

多的这个权限回调在哪执行 OnPermissionDenied 这个会生成2组回调么 我这里 只生成了一组

wilder-ness avatar Apr 10 '20 12:04 wilder-ness

@TankJK thank you we could replicate the issue.

Actually the root cause is ACCESS_BACKGROUND_LOCATION doesn't exist but you request the permission to Android and it always return -1(DENIED). Kind of obvious but this can be an improvement point for our library, let us think a bit.

A quick workaround is like below on your side.

@NeedsPermission(Manifest.permission.ACCESS_FINE_LOCATION)
fun method1() = TODO

@RequiresApi(api = Build.VERSION_CODES.Q)
@NeedsPermission(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
fun method2() = TODO

fun test() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            method2WithPermissionCheck()
        } else {
            method1WithPermissionCheck()
        }
}

Ref:

  • https://developer.android.com/training/location/receive-location-updates
  • https://github.com/permissions-dispatcher/PermissionsDispatcher/compare/prototype/background_permission?expand=1

I have used this code for requesting the background location and it works as expected but what if I want to check if user has denied the background location permission by selecting "ALLOW ONLY WHILE USING THE APP"? I am trying to use @OnPermissionDenied(Manifest.permission.ACCESS_BACKGROUND_LOCATION) but library doesn't create the dispatcher method and not getting the callback. Please suggest some solution.

prash6001 avatar Apr 20 '20 17:04 prash6001

and READ_PHONE_STATE the same. i have allowed READ_PHONE_STATE permission,but it still crashed,with exception: java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=0, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.centchain.changyo.speeder/com.centchain.changyo.ui.SpeederActivity}: java.lang.SecurityException: getUniqueDeviceId: The user 10452 does not meet the requirements to access device identifiers.

DengFX2010105421 avatar May 21 '20 07:05 DengFX2010105421

JFYI now we're considering to prep a dedicated method in ktx module: https://github.com/permissions-dispatcher/PermissionsDispatcher/pull/679

@DengFX2010105421 do you mean READ_PHONE_NUMBERS from Android 11? https://developer.android.com/preview/privacy/permissions#phone-numbers

hotchemi avatar Jul 24 '20 11:07 hotchemi

Same issue for ACCESS_MEDIA_LOCATION permission introduced in API level 29.

Feature request: ability to specify API level for each requested permission inside "@NeedsPermission" annotation.

look1n avatar Nov 07 '21 09:11 look1n