How permission can be checked at runtime without throwing SecurityException? How permission can be checked at runtime without throwing SecurityException? android android

How permission can be checked at runtime without throwing SecurityException?


You can use Context.checkCallingorSelfPermission() function for this. Here is an example:

private boolean checkWriteExternalPermission(){    String permission = android.Manifest.permission.WRITE_EXTERNAL_STORAGE;    int res = getContext().checkCallingOrSelfPermission(permission);    return (res == PackageManager.PERMISSION_GRANTED);            }


This is another solution as well

PackageManager pm = context.getPackageManager();int hasPerm = pm.checkPermission(    android.Manifest.permission.WRITE_EXTERNAL_STORAGE,     context.getPackageName());if (hasPerm != PackageManager.PERMISSION_GRANTED) {   // do stuff}


You can also use this:

private boolean doesUserHavePermission(){    int result = context.checkCallingOrSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);    return result == PackageManager.PERMISSION_GRANTED;}