4

when i start a google maps navigation it keeps the screen alive and if i turn it off and on again with the power button, the maps app is above the lockscreen and only when i minimize the app, i have to unlock the screen. So i can turn the screen off and on and still use the app without having to unlock the screen

How can i achieve that for my own app?

I know i cann keep the screen on with

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

but that doesn't prevent me from having to unlock the screen after turning it off and on again.

ColdFire
  • 6,764
  • 6
  • 35
  • 51
Ginso
  • 459
  • 17
  • 33

3 Answers3

3

You can use something like the screen pinning option. The application will be over the lock screen but it's not done programmatically, however it does the effect you want.

Instructions to pin an app on the screen:

  • Go to Settings →Security→Screen pinning
  • Enable screen pinning and also the option "Ask for unlock pattern before unpinning"
  • Now open the app you want, then switch over to the overview menu
  • Tap on the pin icon on the app.

However, if you want to do it with code, see the attribute : android:showOnLockScreen="true" and the link :

Arnauld Alex
  • 339
  • 1
  • 3
  • 13
3

You should move getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) below setContentView() and put android:keepScreenOn="true" into the root View of your main layout.

  • still the same. It keeps the screen on, but if i turn it off and on again with the power button, i see only the lockscreen – Ginso May 30 '18 at 09:55
0

ok, thanks to the answer given by Arnauld Alex, i got it. Just add

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

before the setContentView(...)

Ginso
  • 459
  • 17
  • 33