2

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!

2 Answers2

2

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.

levan
  • 440
  • 1
  • 8
  • 19
1

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

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • But then how can I request permission for install packages? Because when I try to install the package, it throws me an error message saying permission denied and therefore I needed to request for it –  Jun 11 '18 at 01:02
  • @guest176969 have you checked this [SO](https://stackoverflow.com/questions/4604239/install-application-programmatically-on-android) – Sagar Jun 11 '18 at 01:05
  • May I know what is the goToMarket intent is for? Also, I tried the promptInstall intent but it does not explicitly request the user for permission –  Jun 11 '18 at 01:07
  • @guest176969 to redirect user to play store and allow him/her to download the app from play store – Sagar Jun 11 '18 at 01:08
  • I have tried this: https://stackoverflow.com/questions/50789156/android-install-apk-programatically-not-working –  Jun 11 '18 at 01:08
  • @guest176969 you can try the answer which I have recommended and let me know if you face issues. – Sagar Jun 11 '18 at 01:10
  • As in the hyperlink? I tried that one but it does not work that is why I am using the FileProvider approach –  Jun 11 '18 at 01:12
  • I removed the if statement but it still not prompting me for the permission –  Jun 11 '18 at 01:16
  • @guest176969 As I said, it won't prompt because those permissions are not meant for third party apps. – Sagar Jun 11 '18 at 01:18
  • Do you mind to provide me with some example? What do you mean by intent, I am lost. –  Jun 11 '18 at 01:19
  • @guest176969 I mean, follow the SO that I have recommended and you have tried. Check the cause of issue on the other question. You can check my comment on your another question. – Sagar Jun 11 '18 at 01:21
  • @guest176969 For the context for this question, the answer is what I have mentioned. If you think it was useful, then remember to mark it as accepted. – Sagar Jun 11 '18 at 01:42