Search code examples
androidkotlinpermissionsoppo

Android 11 Runtime Permissions


Before a few days, the code is working fine not having issues with permissions.

I am not able to grant permission at run-time also having issues while granting permissions from the setting. (App permission detail page).

var permissions = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
        arrayOf(Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
    } else {
        arrayOf(Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)
    }

Requesting For Permissions on the appropriate button click.

permissionResultLauncher =
        registerForActivityResult(
                ActivityResultContracts.RequestMultiplePermissions()
        ) { permissions ->
            var allPermissionGranted = true
            permissions.entries.forEach {
                if (!it.value) {
                    Log.e(TAG, "Permission ${it.key} granted : ${it.value}")
                    allPermissionGranted = false
                }
            }
            if (!permissionDeniedDialog && !allPermissionGranted) {
                showDialog(
                        "Required",
                        "App needs Bluetooth and Location permissions to scan bluetooth devices.",
                        "Later",
                        "Allow",
                        object : DialogView.ButtonListener {
                            override fun onNegativeButtonClick(dialog: AlertDialog) {
                                dialog.dismiss()
                                val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                                        Uri.fromParts("package", requireActivity().applicationContext.packageName, null))
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                startActivity(intent)
                            }

                            override fun onPositiveButtonClick(dialog: AlertDialog) {
                                dialog.dismiss()
                                requestRequirdPermissions()
                            }
                        })
            } else {
                val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                        Uri.fromParts("package", requireActivity().applicationContext.packageName, null))
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                startActivity(intent)
            }
            permissionDeniedDialog = true
        }
permissionResultLauncher.launch(permissions)

Function for checking permissions

private fun checkPermission(): Boolean {
        for (permission in permissions) {
            if (ContextCompat.checkSelfPermission(requireContext(), permission) != PackageManager.PERMISSION_GRANTED) return false
        }
        return true
    }

Can someone let me know why the above not working?. the system was on the result directly and the app was redirecting the app to the setting page, but I am not able to grant permission from the settings page too.

Is this OS Specific?, anyone having the same issues with ColorOS 11 ?. Please guide me if anything is missing from my side.

Device: OPPO F17 Pro OS: Color OS 11, Android 11 Based

Note:

Above code, with Samsung device, Android 11 Based (OneUI 3.1), App was not asking for runtime but after redirecting on setting page, I am granting location permission and app was working fine, regarding OPPO i am not able grant permission from setting page.

What tried :

  var permissions = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
    arrayOf(Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
} else {
    arrayOf(Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_COARSE_LOCATION)
}


private fun requestRequirdPermissions() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
            permissionResultLauncher.launch(arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION))
        }
        else
        {
            permissionResultLauncher.launch(arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION ))
        }
    }

Solution

  • My issue was fixed by the system reset, but according to comments and documents please follow the below guidelines for the location permission.

    Thanks Pawel for comments,

    Ref: https://stackoverflow.com/a/66321942/9909365

    Background location permission does not work like other permissions. It's a request to elevate location permission from foreground-only to foreground & background.

    User has to consciously select "Allow all the time" in order to do that and grant background location permission. Otherwise that permission is considered denied.

    You cannot even request background location unless foreground location is already granted - when system permission activity shows up it should already have option 2 or 3 selected.

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

    Note:

    When your app is using this background location please be prepared with a short video that demonstrates the location-based feature in your app that requires access to location in the background (while the app is not in use).

    See https://support.google.com/googleplay/android-developer/answer/9799150