android programs a method to determine whether an application has a permission

  • 2020-10-07 18:52:19
  • OfStack

This article illustrates how android programming can determine whether an application has a certain permission. To share for your reference, the details are as follows:

android sometimes USES the following methods in development to determine whether an application has permissions or to obtain a list of permissions for an application

1) Determine whether the application has a certain permission


PackageManager pm = getPackageManager(); 
boolean permission = (PackageManager.PERMISSION_GRANTED == 
  pm.checkPermission("android.permission.RECORD_AUDIO", "packageName")); 
if (permission) {
 showToast(" I have that access "); 
}else {
 showToast(" I don't have this right "); 
}

2) Get the list of permissions for an application


try {
 PackageInfo pack = pm.getPackageInfo("packageName",PackageManager.GET_PERMISSIONS);</span> 
 String[] permissionStrings = pack.requestedPermissions;
 showToast(" Permissions list --->" + permissionStrings.toString());
} catch (NameNotFoundException e) {
 e.printStackTrace();
}

permissionStrings is an array of such strings


[android.permission.INTERNET,
 android.permission.READ_PHONE_STATE,
 android.permission.READ_CONTACTS,
  ... 
 android.permission.READ_EXTERNAL_STORAGE,
 android.permission.READ_CALL_LOG,
 android.permission.WRITE_CALL_LOG]

I hope this article has been helpful in Android programming.


Related articles: