PermissionsDispatcher
PermissionsDispatcher copied to clipboard
ACCESS_BACKGROUND_LOCATION permission has issue when API less than Android Q
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 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 Yes all permission is been added.
@TankJK really? I wonder because ACCESS_BACKGROUND_LOCATION
is from API level 29?
@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:
- While using app (which is ACCESS_FINE_LOCATION)
- 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.
we'll take a look
@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 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
问题。。回调了怎么办。 请求权限分成2个方法。回调的时候
多的这个权限回调在哪执行 OnPermissionDenied 这个会生成2组回调么 我这里 只生成了一组
@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.
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.
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
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.