user9555243
user9555243

Reputation: 155

How to check if app has a specific permission?

I am trying to check if my app has access to storage:

 if (ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(getBaseContext(), "merry christmas", Toast.LENGTH_LONG).show();
    }

this only works when the storage permission is not in the manifest. It stops showing toast when i add the permission. Seems like this code checks if the manifest has the permission instead of if the permission is granted. How do I fix this???

Upvotes: 0

Views: 3031

Answers (4)

user10611404
user10611404

Reputation:

Add the following dependency in app level gradle and sync your project.

implementaion 'com.nabinbhandari.android:permissions:3.8'

After that inside your activity write the code to check if permission is granted.

 public void requestCameraAndStorage() {
    String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA}; // dont forget to add these permissions in manifest file.
    Permissions.check(this, permissions, null, null, new PermissionHandler() {
        @Override
        public void onGranted() {
            // do whatever you want to do.
        }

        @Override
        public void onDenied(Context context, ArrayList<String> deniedPermissions) {
            super.onDenied(context, deniedPermissions);
        }
    });
}

And call this method inside onCreate or at any onButtonClick as want.This library handles onDenied very efficiently.

Upvotes: 0

Raza
Raza

Reputation: 809

You have to manually ask the user for allowing permission with following code. And then check that if permission is granted or not.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(permitted()){
        // prompting permission dialog
        // permform your task
     }

 }


@Override
public void onResume() {
    super.onResume();
    if (permitted(PERMISSIONS))
        load();
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 1:
            if (permitted(permissions)) {
                load();
            } else {
                Toast.makeText(context, R.string.not_permitted, Toast.LENGTH_SHORT).show();
            }
    }
}
public static final String[] PERMISSIONS = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

boolean permitted(String[] ss) {
    for (String s : ss) {
        if (ContextCompat.checkSelfPermission(context, s) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
    }
    return true;
}

boolean permitted() {
    for (String s : PERMISSIONS) {
        if (ContextCompat.checkSelfPermission(context, s) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(), PERMISSIONS, 1);
            return false;
        }
    }
    return true;
}

Upvotes: 2

Kevin Kurien
Kevin Kurien

Reputation: 842

This is how I do it

   if (!checkPermission()) {  // this line checks permission everytime you access this activity
            Toast.makeText(context, R.string.not_permitted, Toast.LENGTH_SHORT).show();
       requestPermission();
   } else {
       enableGPS();
   }  




public boolean checkPermission() {
        int result = ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION);
        return result == PackageManager.PERMISSION_GRANTED;
    }

    public void requestPermission() {
        ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS);
    }  

@Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        if (requestCode == REQUEST_CODE_ASK_PERMISSIONS) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                enableGPS();
            } else {
                Toast.makeText(getApplicationContext(), "Please allow access to continue!", Toast.LENGTH_SHORT).show();
            }
        }
    }

Upvotes: 0

RoyalGriffin
RoyalGriffin

Reputation: 2007

Your code only works if the permission is not granted. If it didn't run then that permission was granted either during installation or from the data of the previous apk. Just go to settings, choose your app and revoke the permission or select the clear data option and try again.

Upvotes: 0

Related Questions