Android 6.0以上获取悬浮窗权限

Android 6.0以上获取悬浮窗权限

public static boolean checkFloatingPermission(Context context){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //6以上机型需要动态授权悬浮窗权限
            return Settings.canDrawOverlays(context);
        }else{
            //6以下机型默认会授权悬浮窗权限,但有些rom会有问题,所以这里通过反射去判断是否已经授权
            return checkOp(context);
        }
    }
public static boolean checkOp(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
            try {
                Method method = AppOpsManager.class.getDeclaredMethod("checkOp", int.class, int.class, String.class);
                return AppOpsManager.MODE_ALLOWED == (int) method.invoke(manager, OP_SYSTEM_ALERT_WINDOW, Binder.getCallingUid(), context.getPackageName());
            } catch (Exception e) {
            }
        }
        return false;
    }

你可能感兴趣的:(Android,功能实现,常见功能,android)