简体   繁体   English

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

[英]Settings.canDrawOverlays is returning false even after turning the permission on from settings

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. 但即使从设置启用了权限,它也会返回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;  }

}

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

does the check involves the device admin? 检查是否涉及设备管理员? I have encountered this problem when disabling device admin, I have checked this permission in the DeviceAdminReceiver->onDisabled() and on some devices, and canDrawOverlays returned false, despite the fact i had the permission. 我在禁用设备管理员时遇到此问题,我已在DeviceAdminReceiver-> onDisabled()和某些设备上检查此权限,并且canDrawOverlays返回false,尽管我已获得该权限。

The above answer helped sometimes but not all the time. 上述答案有时帮助但不是所有时间。 the thing that did work is Thread.sleep before the check. 在检查之前做的工作是Thread.sleep。

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

The minimal time that worked for me was 20 millis. 对我有用的最短时间是20毫升。 than canDrawOverlays returned true 比canDrawOverlays返回true

Note: this is not a good practice however this is the only thing that worked for me 注意:这不是一个好习惯,但这是唯一对我有用的东西

Based on BennyP 's answer, I've made a Runnable run the required code after 500ms and that worked very well. 基于BennyP的回答,我已经让Runnable在500ms之后运行了所需的代码并且运行良好。 The feedback is a bit delayed, but the user won't even notice the delay. 反馈有点延迟,但用户甚至不会注意到延迟。

This is the code I've added to my onResume() 这是我添加到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);

Hope it helps! 希望能帮助到你!

I tried restarting the activity after the user accessed the setting . 我尝试在用户访问设置后重新启动活动。 This is code : 这是代码:

public static void restartActivity(Activity act){ public static void restartActivity(Activity act){

Intent intent = getIntent(); Intent intent = getIntent();

finish(); 完();

startActivity(intent); startActivity(意向);

} }

First of all, I am really very surprised with this strange behaviour of 首先,我对这种奇怪的行为感到非常惊讶

    Settings.canDrawOverlays(this);        

I also faced the same issue with its usage, it was returning false even if the permission is already assigned. 我的使用也遇到了同样的问题,即使已经分配了权限,它也会返回false。 What I noticed that, I was using this check in my onStart() method, where it was creating this wired behavior. 我注意到了,我在onStart()方法中使用了这个检查,它正在创建这种有线行为。 To resolve this, I searched over internet and no result was there that can satisfy me and the one I can use. 为了解决这个问题,我搜索了互联网,没有任何结果可以满足我和我可以使用的那个。

Solution

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

I did something lake this, in my onCreate(). 我在onCreate()中做了一些湖。 Here I saved the values accordingly in my SharedPreferences, and according to these Shared Preference values, I created a check for showing an overlay dialog in my onStart(). 在这里,我在SharedPreferences中相应地保存了值,并根据这些共享首选项值,我创建了一个检查,用于在我的onStart()中显示叠加对话框。 This worked fine! 这很好用!

You can try this solution, and mark this answer useful if your problem is solved. 您可以尝试此解决方案,如果您的问题得到解决,请将此答案标记为有用。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Settings.canDrawOverlays权限授予问题 - Settings.canDrawOverlays permission grant issue API的设置.canDrawOverlays <23 - Settings.canDrawOverlays for API < 23 Settings.canDrawOverlays(context)在Android Oreo上返回false - Settings.canDrawOverlays(context) returns false on Android Oreo 当用户授予绘制叠加层的权限并返回到我的应用程序时,为什么在 Android 8 方法 Settings.canDrawOverlays() 中返回“false”? - Why in Android 8 method Settings.canDrawOverlays() returns “false” when user has granted permission to draw overlays and returns to my application? 从设置打开后无法锁定GPS - Failing to lock on to GPS after turning it on from settings 从“设置Api”对话框中打开位置后,getLastLocation()返回null - getLastLocation() returns null after turning on location from Settings Api Dialog 即使允许,权限也会返回 false - Permission returning false even when allowed 从“设置”片段返回后,杀死活动中的媒体播放器 - Kill mediaplayer in an Activity after returning from Settings Fragment 即使授予权限,checkSelfPermission 也会返回 false - checkSelfPermission returning false even if Permission is granted 进入设置后返回主活动 - Returning to the main Activity after going in settings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM