AppOpsManager Sdk 23 以上才可以检查

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public boolean lacksPermissionOps(String permission){

    String checkStr="";

    switch (permission){
        case Manifest.permission.CAMERA :
            checkStr= AppOpsManager.OPSTR_CAMERA;
            break;
        case Manifest.permission.READ_EXTERNAL_STORAGE:
            checkStr= AppOpsManager.OPSTR_READ_EXTERNAL_STORAGE;
            break;
        case Manifest.permission.WRITE_EXTERNAL_STORAGE:
            checkStr= AppOpsManager.OPSTR_WRITE_EXTERNAL_STORAGE;
            break;
        case Manifest.permission.ACCESS_COARSE_LOCATION:
            checkStr= AppOpsManager.OPSTR_COARSE_LOCATION;
            break;
        case Manifest.permission.ACCESS_FINE_LOCATION:
            checkStr= AppOpsManager.OPSTR_FINE_LOCATION;
            break;
        case Manifest.permission.READ_PHONE_STATE:
            checkStr= AppOpsManager.OPSTR_READ_PHONE_STATE;
            break;
        case Manifest.permission.RECORD_AUDIO:
            checkStr= AppOpsManager.OPSTR_RECORD_AUDIO;
            break;
        case Manifest.permission.MODIFY_AUDIO_SETTINGS:
            checkStr= AppOpsManager.OPSTR_RECORD_AUDIO;
            break;
        case Manifest.permission.CALL_PHONE:
            checkStr= AppOpsManager.OPSTR_CALL_PHONE;
            break;
        case Manifest.permission.READ_CONTACTS:
            checkStr= AppOpsManager.OPSTR_READ_CONTACTS;
            break;
        case Manifest.permission.WRITE_CONTACTS:
            checkStr= AppOpsManager.OPSTR_WRITE_CONTACTS;
            break;
    }

    if(TextUtils.isEmpty(checkStr)){
        return false;
    }

    final int version = Build.VERSION.SDK_INT;

    if (version >= 23) {
        AppOpsManager manager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);

        int checkResult = manager.checkOpNoThrow(
                checkStr,
                Binder.getCallingUid(),
                mContext.getPackageName()
        );

        if (AppOpsManager.MODE_ALLOWED == checkResult) {
            return false;
        } else {
            return true;
        }
    }

    return false;

}

你可能感兴趣的:(权限,android,AndroidBUGFix)