1. 程式人生 > >動態獲取許可權工具類(方法)

動態獲取許可權工具類(方法)

直接上程式碼,大家應該能看懂
    private static List<String> permissionList;
    public static void getPermission(Activity context,String... permission) {
        permissionList = new ArrayList<>();
        for (int i = 0;i<permission.length;i++){
            if (ContextCompat.checkSelfPermission(context,permission[i])
                    != PackageManager.PERMISSION_GRANTED){
                permissionList.add(permission[i]);
            }
        }
        if (permissionList.size()>0){
            String[]  permissions = new String[permissionList.size()];
            for (int i = 0;i<permissionList.size();i++){
                permissions[i] = permissionList.get(i);
            }
            ActivityCompat.requestPermissions(context, permissions,1);
        }
    }

把上面的程式碼複製到自己的工具類裡面然後呼叫就行了,呼叫方式

BaseTool.getPermission(this, android.Manifest.permission.CAMERA,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                android.Manifest.permission.ACCESS_FINE_LOCATION);

如果有什麼問題希望大家共同探討