SYSTEM_ALERT_WINDOW PERMISSION on API 26 not working as expected. Permission denied for window type 2002

后端 未结 7 1915
轮回少年
轮回少年 2020-12-08 21:17

I am using overlay permission to display certain information in my app. Running it on API 23 - 25 it works fine (asking for permission, granting, etc. according to

U

相关标签:
7条回答
  • 2020-12-08 21:31
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
    WindowManager.LayoutParams.TYPE_SYSTEM_ERROR
    }
    else
    {
    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
    }
    

    only change in this parameter of Window manager

    0 讨论(0)
  • 2020-12-08 21:32

    Android Oreo (and future versions) doesn't allow use WindowManager.LayoutParams.TYPE_PHONE because is deprecated instead use WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                params = new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.TYPE_PHONE,
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                        PixelFormat.TRANSLUCENT);
            }else{
                params = new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                        PixelFormat.TRANSLUCENT);
            }
    }
    
    0 讨论(0)
  • 2020-12-08 21:35

    According to the documentation on Android 8.0 Behavior Changes for apps targeting Android 8.0:

    Apps that use the SYSTEM_ALERT_WINDOW permission can no longer use the following window types to display alert windows above other apps and system windows:

    TYPE_PHONE
    TYPE_PRIORITY_PHONE
    TYPE_SYSTEM_ALERT
    TYPE_SYSTEM_OVERLAY
    TYPE_SYSTEM_ERROR
    

    Instead, apps must use a new window type called TYPE_APPLICATION_OVERLAY.

    So your app could target some lower version. In this case, your alert window will ...

    always appear beneath the windows that use the TYPE_APPLICATION_OVERLAY window type. If an app targets Android 8.0 (API level 26), the app uses the TYPE_APPLICATION_OVERLAY window type to display alert windows.

    (quoted from the same source)

    0 讨论(0)
  • 2020-12-08 21:36

    Instead of going directly on your Page, you must request for permission first something like this (in my project main page is target page) you can find more in FIX PERMISSION DENIED FOR THIS WINDOW TYPE

    private static final int OVERLAY_PERMISSION_CODE =5463 ; // can be anything
    
    @Override
    public void onCreate(...) {
    
        if (Build.VERSION.SDK_INT >= 23) {
            if (!Settings.canDrawOverlays(this)) {
    
                // Open the permission page
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, OVERLAY_PERMISSION_CODE);
                return;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-08 21:37
      int LAYOUT_FLAG;
    
        mOverlayView = LayoutInflater.from(this).inflate(R.layout.overlay_layout, null);
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
        }
    
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                LAYOUT_FLAG,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
    
    0 讨论(0)
  • 2020-12-08 21:46

    try this code working perfect

     int layout_parms;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             layout_parms = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
                layout_parms = WindowManager.LayoutParams.TYPE_PHONE;
        }
    
        yourparams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                layout_parms,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
    
    0 讨论(0)
提交回复
热议问题