user7691120
user7691120

Reputation:

Android runtime permission keep getting denied

I am trying to request runtime permission for my Android application. Here is my code:

private static final int PERMISSION_REQUEST_CODE = 1;
String installPermission = Manifest.permission.INSTALL_PACKAGES;
String writePermission = Manifest.permission.WRITE_EXTERNAL_STORAGE;

@Click(R.id.buttonVersionUpgrade)
void buttonVersionUpgradeClicked(View v) {
    if (!checkPermission(installPermission)) {
        requestPermission(installPermission);
    }
    if (!checkPermission(writePermission)) {
        requestPermission(writePermission);
    }
}

private boolean checkPermission(String permission){
    if (Build.VERSION.SDK_INT >= 23) {
        int result = ContextCompat.checkSelfPermission(getApplicationContext(), permission);
        if (result == PackageManager.PERMISSION_GRANTED){
            return true;
        } else {
            return false;
        }
    } else {
        return true;
    }
}

private void requestPermission(String permission){
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)){
        Toast.makeText(getApplicationContext(), "Permission required for features to work.",Toast.LENGTH_LONG).show();
    }
    ActivityCompat.requestPermissions(this, new String[]{permission},PERMISSION_REQUEST_CODE);
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permission Granted.",
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(),"Permission Denied.",
                        Toast.LENGTH_LONG).show();
            }
            break;
    }
}

In my AndroidManifest.xml:

<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

However, the code above keep showing permission denied without prompting the user for permission. Any ideas?

Thanks!

Upvotes: 2

Views: 907

Answers (2)

levan
levan

Reputation: 490

I had an error in manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE " />

I had space after "WRITE_EXTERNAL_STORAGE". I deleted it, and everything is OK now.

Upvotes: 2

Sagar
Sagar

Reputation: 24907

First of all if (Build.VERSION.SDK_INT >= 23) {...} is unnecessary since ContextCompat.checkSelfPermission already takes care of necessary backward compatibility.

However, the code above keep showing permission denied without prompting the user for permission.

This is because, android.permission.INSTALL_PACKAGES & android.permission.DELETE_PACKAGES are not for use by third-party applications.

If you only request android.permission.WRITE_EXTERNAL_STORAGE then you should see the proper permission dialog.

Permission request is shown for the permissions whose protection level is dangerous. You can refer this document to determine the protection level. Refer this permission overview document for more details regarding permission levels and types

Upvotes: 1

Related Questions