android判断应用是否有某个权限

android在开发中有时候要判断应用中是否有某项权限,或者想获取到某个应用的权限清单,可以使用以下方法



1) 判断应用是否具有某个权限

[java]  view plain copy
  1. PackageManager pm = getPackageManager();  
  2.         boolean permission = (PackageManager.PERMISSION_GRANTED ==   
  3.                 pm.checkPermission("android.permission.RECORD_AUDIO""packageName"));  
  4.         if (permission) {  
  5.             showToast("有这个权限");  
  6.         }else {  
  7.             showToast("木有这个权限");  
  8.         }  


 
  2)获取某个应用的权限清单 
   
  

[java]  view plain copy
  1. try {  
  2.             PackageInfo pack = pm.getPackageInfo("packageName",PackageManager.GET_PERMISSIONS);  
  3.             String[] permissionStrings = pack.requestedPermissions;  
  4.             showToast("权限清单--->" + permissionStrings.toString());  
  5.         } catch (NameNotFoundException e) {  
  6.             e.printStackTrace();  
  7.         }  


 
  
[java]  view plain copy
  1. permissionStrings 是一个这样的字符串数组  
[java]  view plain copy
  1. [android.permission.INTERNET,   
  2.          android.permission.READ_PHONE_STATE,   
  3.          android.permission.READ_CONTACTS,   
  4.          ……  
  5.          android.permission.READ_EXTERNAL_STORAGE,   
  6.          android.permission.READ_CALL_LOG,   
  7.          android.permission.WRITE_CALL_LOG]  

 
  

 
  

你可能感兴趣的:(android判断应用是否有某个权限)