Karumi / Dexter

Android library that simplifies the process of requesting permissions at runtime.
http://karumi.com
Apache License 2.0
5.23k stars 671 forks source link

API 30 Android 11: Requesting background location permission #281

Open dds861 opened 3 years ago

dds861 commented 3 years ago

Expected behaviour

Show the same permission dialog as in API 29

Actual behaviour

Nothing happens. Permission dialog is being ignored

If your app targets Android 11 or higher, the system enforces this best practice. If you request a foreground location permission and the background location permission at the same time, the system ignores the request and doesn't grant your app either permission. https://developer.android.com/about/versions/11/privacy/location#request-background-location-separately

https://developer.android.com/training/location/permissions#request-background-location

As i understand, i have to ask user for foreground location permission first, and then after giving permission i have to ask again for a background permission...

Steps to reproduce

in build.gradle change targetSdkVersion 30 and compileSdkVersion 30 use emulator API 30

just copy to mainActivity and call requestAccessFineLocation() from onCreate(...)

private fun requestAccessFineLocation() {
        Dexter.withContext(this)
            .withPermissions(createPermissionList1())
            .withListener(object : MultiplePermissionsListener {
                override fun onPermissionsChecked(report: MultiplePermissionsReport) {
                    Log.i("myApp", "onPermissionsChecked1 = " + report.areAllPermissionsGranted())

                }

                override fun onPermissionRationaleShouldBeShown(
                    permissions: MutableList<PermissionRequest>,
                    token: PermissionToken
                ) {

                    Log.i("myApp", "onPermissionRationaleShouldBeShown1")
                    token.continuePermissionRequest()
                }

            }).check()
    }
    private fun createPermissionList1(): MutableList<String> {
        val permissionList = mutableListOf(
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_BACKGROUND_LOCATION

        )
        return permissionList
    }

Version of the library

implementation 'com.karumi:dexter:6.2.2' or implementation 'com.karumi:dexter:4.2.0' (i changed withContext(...) to withActivity(...))