Search code examples
androidtimeoutoverlay

TYPE_STATUS_BAR overlay makes system ignore screen timeout


Goodmorning, i'm developing an overlay which is posted by a service. The problem is that the screen stays always on, even after screeen timeout expires.

This is my code:

WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
            WindowManager.LayoutParams.TYPE_STATUS_BAR|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
    params.gravity= Gravity.TOP | Gravity.LEFT;
            overlay.setKeepScreenOn(false);
            wm.addView(overlay, params);

I've already tried to add WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON, but no luck.

Thanks for your help.


Solution

  • The solution is not to use WindowManager.LayoutParams.TYPE_STATUS_BAR but one with lower privileges as this is only used by the system.