Android工具类-权限工具类

记录一下自己写的权限工具类

public class PermissionUtil {

    //requestCode
    public static final int REQUEST_CODE_ONE = 1;
    public static final int REQUEST_CODE_TWO = 2;
    public static final int REQUEST_CODE_THREE = 3;

    /**
     *
     * 常用权限
     *
     * 定位权限
     * Manifest.permission.ACCESS_COARSE_LOCATION
     * 打电话权限
     * Manifest.permission.CALL_PHONE
     * 读写文件权限
     * Manifest.permission.WRITE_EXTERNAL_STORAGE
     * 读取电话状态
     * Manifest.permission.READ_PHONE_STATE
     * 摄像头权限
     * Manifest.permission.CAMERA
     */

    //存放需要的权限
    public static String[] permissions = {
            Manifest.permission.CALL_PHONE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA
    };

    /**
     * 请求权限
     * @param activity
     * @param permissions
     */
    public static void requestPermissions(Activity activity , String[] permissions){
        ActivityCompat.requestPermissions(activity,permissions,REQUEST_CODE_ONE);
    }

    /**
     * 解释权限 (是否点击不在提醒)
     * @param activity
     * @param permission
     * @return true 没点  false 点了
     */
    public static boolean shouldShowPermissions(Activity activity , String permission){
        return ActivityCompat.shouldShowRequestPermissionRationale(activity,permission);
    }

    /**
     * 查询是否拥有某个权限
     * @param context
     * @param permission
     * @return true 有 false 没有
     */
    public static boolean checkPermissions(Context context , String permission){
        int code = ContextCompat.checkSelfPermission(context,permission);
        if (code == PackageManager.PERMISSION_GRANTED)
            return true;
        else
            return false;
    }

}

你可能感兴趣的:(Android,工具类)