Android O - FLAG_SHOW_WHEN_LOCKED is deprecated

Elior picture Elior · Jan 16, 2018 · Viewed 18.9k times · Source

I'm targetting my application to Android O. In my application I have a job service that shows a window over all other applications, so when it triggered it needs to show this window even when the screen is turned off & unlocked and turn it on. I've achieved this behaviour in preior Android versions, but in Android O it doesn't work as I expected.

I've read that I need to use the flag TYPE_APPLICATION_OVERLAY and added also the permission <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>in the manifest file.

So it works fine when the screen is on, but when the screen is off I've noticed that it doesn't turn the screen on, and when I turned the screen I saw that the window was created on top of other applications.

So my question is since the flags FLAG_TURN_SCREEN_ON and FLAG_SHOW_WHEN_LOCKED are deprecated in Android API 27, what are the alternatives way of doing that?

this is my current code:

private void showView()
{
    if (!wakeLockAcquired)
    {
        wakeLock.acquire();
        wakeLockAquired = true;
    }

    windowManager = (WindowManager)context.getSystemService(WINDOW_SERVICE);

    final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0, 0,
            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
            ,
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON ,
            PixelFormat.RGBA_8888);

    RelativeLayout layout = buildView();
    windowManager.addView(layout, layoutParams);
    windowManager.updateViewLayout(layout, layoutParams);
}

Notes:

  • buildView is a function that returns a relativeLayout, in this function I'm adding the content dynamically (TextView, ImageView, etc...).
  • wakeLockAcquired is a boolean member, and it sets to false when the view is destroyed.

Answer

Dmitry Ognyov picture Dmitry Ognyov · May 3, 2018

KeyguardManager turn on screen if attr turnScreenOn is true, so order of methods and call requestDismissKeyguard is necessary. I use this code for activity, hope it'll help:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
        setShowWhenLocked(true)
        setTurnScreenOn(true)
        val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
        keyguardManager.requestDismissKeyguard(this, null)
    } else {
        this.window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
    }
}