即使在设置中启用权限后,Settings.canDrawOverlays仍返回false

问题描述 投票:3回答:5

我正在尝试从this回答浪潮代码来检查是否启用了权限。但即使从设置启用了权限,它也会返回false。

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;  }

}
android overlay system-alert-window
5个回答
5
投票

最近我也遇到了同样的问题并得到了以下解决方法。引自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;
}

1
投票

检查是否涉及设备管理员?我在禁用设备管理员时遇到此问题,我已在DeviceAdminReceiver-> onDisabled()和某些设备上检查此权限,并且canDrawOverlays返回false,尽管我已获得该权限。

上述答案有时帮助但不是所有时间。在检查之前做的工作是Thread.sleep。

try {
    Thread.sleep(20);
} catch (InterruptedException e) {
    // some exception here
}

对我有用的最短时间是20毫升。比canDrawOverlays返回true

注意:这不是一个好习惯,但这是唯一对我有用的东西


0
投票

根据BennyP的回答,我已经让Runnable在500ms之后运行了所需的代码并且运行良好。反馈有点延迟,但用户甚至不会注意到延迟。

这是我添加到我的onResume()的代码

Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if(!Settings.canDrawOverlays(ControllerActivity.this)){
                //Handle overlay permission not given here
            }
            else{
                //Handle overlay permission given here
            }
        }
    }, 500);

希望能帮助到你!


0
投票

我尝试在用户访问设置后重新启动活动。这是代码:

public static void restartActivity(Activity act){

Intent intent = getIntent();

完();

startActivity(意向);

}


-1
投票

首先,我对这种奇怪的行为感到非常惊讶

    Settings.canDrawOverlays(this);        

我的使用也遇到了同样的问题,即使已经分配了权限,它也会返回false。我注意到了,我在onStart()方法中使用了这个检查,它正在创建这种有线行为。为了解决这个问题,我搜索了互联网,没有任何结果可以满足我和我可以使用的那个。

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        Log.e("Overlay Permission", "" + Settings.canDrawOverlays(this));
        if (!Settings.canDrawOverlays(this)) {
            MyPreferences.saveBoolean(HomeScreen.this, "showOverlayPermissionDialog", true);
        } else {
            MyPreferences.saveBoolean(HomeScreen.this, "showOverlayPermissionDialog", false);
        }
    }

我在onCreate()中做了一些湖。在这里,我在SharedPreferences中相应地保存了值,并根据这些共享首选项值,我创建了一个检查,用于在我的onStart()中显示叠加对话框。这很好用!

您可以尝试此解决方案,如果您的问题得到解决,请将此答案标记为有用。

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.