即使从设置中打开了权限,Settings.canDrawOverlays也返回false [英] Settings.canDrawOverlays is returning false even after turning the permission on from settings

查看:1181
本文介绍了即使从设置中打开了权限,Settings.canDrawOverlays也返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过答案中的翻滚代码来检查权限是否已启用.但是即使从设置中启用了权限,它也会返回false.

I am trying billow Code from this answer to check if the permission is enabled. but it is returning false even when the permission is enabled from the settings.

public static boolean canDrawOverlayViews(Context con){
    if(Build.VERSION.SDK_INT< Build.VERSION_CODES.LOLLIPOP){return true;}
    try {
        return Settings.canDrawOverlays(con);
    }
    catch(NoSuchMethodError e){
        return canDrawOverlaysUsingReflection(con);
    }
}


public static boolean canDrawOverlaysUsingReflection(Context context) {

    try {

        AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        Class clazz = AppOpsManager.class;
        Method dispatchMethod = clazz.getMethod("checkOp", new Class[] { int.class, int.class, String.class });
        //AppOpsManager.OP_SYSTEM_ALERT_WINDOW = 24
        int mode = (Integer) dispatchMethod.invoke(manager, new Object[] { 24, Binder.getCallingUid(), context.getApplicationContext().getPackageName() });

        return AppOpsManager.MODE_ALLOWED == mode;

    } catch (Exception e) {  return false;  }

}

推荐答案

最近,我也遇到了同样的问题,并得到以下解决方法. 引用自 https://code.google.com/p/android/issues /detail?id = 198671#c7

Recently I've also faced the same issue and got the following workaround . Referenced from https://code.google.com/p/android/issues/detail?id=198671#c7

public boolean getWindoOverLayAddedOrNot2() {

   String sClassName = "android.provider.Settings";
   try {
       Class classToInvestigate = Class.forName(sClassName);
       if (context == null)
           context = activity;
       Method method = classToInvestigate.getDeclaredMethod("isCallingPackageAllowedToDrawOverlays", Context.class, int.class, String.class, boolean.class);
       Object value = method.invoke(null, context, Process.myUid(), context.getPackageName(), false);

       Log.i("Tag", value.toString());
       // Dynamically do stuff with this class
       // List constructors, fields, methods, etc.

   } catch (ClassNotFoundException e) {
       // Class not found!
   } catch (Exception e) {
       // Unknown exception
       e.printStackTrace();
   }

   return false;
}

这篇关于即使从设置中打开了权限,Settings.canDrawOverlays也返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆