Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android permission automatically denied on requestPermission from MainActivity

Tags:

android

In the main activity of an Android application I check for permissions (Manifest.permission.MANAGE_DOCUMENTS), detect I do not have them, and call requestPermisions. Then in onRequestPermissionResult I almost immediately get denied permission, without a dialog ever showing.

I already confirmed the same permission in another activity of the same app (through requestPermissions again, which works), so I expected this decision to be stored (for the session, or whatever), and I have never selected to deny the permission. Either way, the permission dialog is not displayed and the permission is denied automatically.

So far I have tested this on emulators of Android 7 and 6 (API 24 and 23).

I have tried:

  • clearing the app's data cache and wiping the device, so it's fresh
  • definitely not this Getting permission denied on Android M
  • triple-checked spelling after seeing this onRequestPermissionsResult returns immediately with denied permission and I am calling the super method
  • a bunch of other suggestions on stack overflow

I'm pretty stumped...

Here is the permission request (see comment in the code):

private fun askForPermissionOrSendRequest(view: View, permission: String) {
    if (checkSelfPermission(permission) == PackageManager.PERMISSION_DENIED) {
        if (shouldShowRequestPermissionRationale(permission)) {
            cachedView = view
            val explanationDialog = AlertDialog.Builder(this).setMessage("We need permissions to read your storage in order to show your profile image.").setOnDismissListener {
                requestPermissions(
                        arrayOf(permission),
                        BSMainActivity.permissionRequestSendProfilePic
                )
            }.create()
            explanationDialog.show()

        } else {
            cachedView = view
            // this branch is always hit - the permission seems to be missing every time
            requestPermissions(
                    arrayOf(permission),
                    BSMainActivity.permissionRequestSendProfilePic
            )
        }
    } else {
        sendRequest(view)
    }
}

I immediately get to the result handler without a dialog showing up to ask me for permissions. I may or may not have confirmed the same permission in another (child) activity of the same app (doesn't seem to make a difference).

override fun onRequestPermissionsResult(requestCode: Int,
                                        permissions: Array<String>,
                                        grantResults: IntArray
) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)

    when (requestCode) {
        BSMainActivity.permissionRequestSendProfilePic -> {
            // This gets hit, MANAGE_DOCUMENTS was denied
            if (permissions.contains(Manifest.permission.MANAGE_DOCUMENTS) && grantResults[permissions.indexOf(Manifest.permission.MANAGE_DOCUMENTS)] == PackageManager.PERMISSION_DENIED) {
                Log.w(logName, "Permission to open image was denied while sending a tag request: %s %s".format(
                        permissions.joinToString(",", "[", "]"),
                        grantResults.joinToString(",", "[", "]")
                ))
            }

            // send request regardless of the result for now
            sendRequest(cachedView)
        }
    }
}

In my manifest I have the following:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="me.monomon.bs">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
like image 782
RecencyEffect Avatar asked Oct 07 '16 22:10

RecencyEffect


People also ask

How do I get permission off Android manifest file?

The simple way to remove this type of permission, is by deleting its xml code line from the original Android Manifest file in android/app/src/main .

How can I tell if an Android user is denied permission?

The method shouldShowRequestPermissionRationale() can be used to check whether the user selected the 'never asked again' option and denied the permission.


1 Answers

Only dangerous permissions can be requested at runtime and MANAGE_DOCUMENTS is not a dangerous permission.

As per the MANAGE_DOCUMENTS documentation:

This permission should only be requested by the platform document management app. This permission cannot be granted to third-party apps.

like image 85
ianhanniballake Avatar answered Oct 20 '22 08:10

ianhanniballake