Android判断APP是否有某一权限

本例判断是否有录音权限,主要是checkPermission方法,PackageInfo中可以获取某个应用的权限清单

PackageManager pm = getPackageManager();  
boolean permission = (PackageManager.PERMISSION_GRANTED ==  
        pm.checkPermission("android.permission.RECORD_AUDIO", "com.lkn.a11509.democollection"));  
if (permission) {  
    Toast.makeText(MainActivity.this,"有录音权限",Toast.LENGTH_SHORT).show();  
}else {  
    Toast.makeText(MainActivity.this,"木有录音权限",Toast.LENGTH_SHORT).show();  
}  
  
try {  
    PackageInfo pack = pm.getPackageInfo("com.lkn.a11509.democollection",PackageManager.GET_PERMISSIONS);  
    String[] permissionStrings = pack.requestedPermissions;  
    String permissionReq = "";  
    for (String permissionString : permissionStrings) {  
        permissionReq = permissionReq + permissionString + "\n";  
    }  
    Toast.makeText(MainActivity.this,"权限清单--->" + permissionReq,Toast.LENGTH_SHORT).show();  
} catch (PackageManager.NameNotFoundException e) {  
    e.printStackTrace(); 
}
对于不同的机型,进入该APP权限管理的方法不同
通过Build.MANUFACTURER来判断,小米和魅族已自测有效,故贴在这里
                        if (Build.MANUFACTURER.equals("Xiaomi")) {  
                            Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");  
                            ComponentName componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");  
                            intent.setComponent(componentName);  
                            intent.putExtra("extra_pkgname", BuildConfig.APPLICATION_ID);  
                            startActivity(intent);  
                        }else if (Build.MANUFACTURER.equals("Meizu")) {  
                            Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");  
                            intent.addCategory(Intent.CATEGORY_DEFAULT);  
                            intent.putExtra("packageName", BuildConfig.APPLICATION_ID);  
                            startActivity(intent);  
                        }

20170817

更好的权限相关方法

https://github.com/Werb/PermissionsCheckerSample

自己Demo的使用

第一步 compile 'com.werb.permissionschecker:permissionschecker:0.0.1-beta2'

第二步 定义好需要检查的权限列表,定义PermissionChecker

    static final String[] PERMISSIONS = new String[]{
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE
    };
    private PermissionChecker permissionChecker;
第三步 初始化
        permissionChecker = new PermissionChecker(this); // initialize,must need

第四步 处理我们的回调方法

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case PermissionChecker.PERMISSION_REQUEST_CODE:
                if (permissionChecker.hasAllPermissionsGranted(grantResults)) {
                    //此处是我们获取权限后的业务逻辑
                } else {
                    permissionChecker.showDialog();
                }
                break;
        }
    }

第五步 在我们需要检查权限的地方

        if (permissionChecker.isLackPermissions(PERMISSIONS)) {
            permissionChecker.requestPermissions();
        } else {
            //检查是否有权限未被获取,有则引导去请求获取,没有则进行我们获取到相应权限后的业务逻辑
        }

你可能感兴趣的:(Android学习)