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

转自:http://blog.csdn.net/lan410812571/article/details/41960425

 

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

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

    PackageManager pm = getPackageManager();  
        boolean permission = (PackageManager.PERMISSION_GRANTED ==   
                pm.checkPermission("android.permission.RECORD_AUDIO", "packageName"));  
        if (permission) {  
            showToast("有这个权限");  
        }else {  
            showToast("木有这个权限");  
        }  

  


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

 

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

  

permissionStrings 是一个这样的字符串数组  
[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]  

  

posted @ 2016-07-07 18:10  凤雏小呆  阅读(998)  评论(0编辑  收藏  举报