Keyguard is shown briefly before Activity is launched when using FLAG_SHOW_WHEN_LOCKED
Asked Answered
L

2

7

I'm using the following flags in onAttachedToWindow() to show my Activity above the keyguard:

FLAG_DISMISS_KEYGUARD | FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON

This works fine, however, when launching my activity from a background service while the screen was off, the keyguard sometimes shows for 1-2 seconds before my app is displayed. This happens particularly on slower phones (or in low memory situations). I find this strange, since my understanding was that onAttachedToWindow() is called after onCreate()/onResume(), so all the "heavy work" should already have been completed when the flags mentioned above are being set? Is there any way to only show my Activity once it has been completely set up?

Lulululuabourg answered 2/10, 2013 at 15:37 Comment(11)
I am also encountering this same issue. Would love an answer.Potation
Are you doing anything particularly heavy in onResume()?Churl
I've put together a sample app using the same flags you're specifying and even on a PoS LG Ally, I can't reproduce the keyguard delay you're describing. Can you post some sample code from your launcher activity and the manifest? Thanks!Katydid
@A--C, yes, both my onCreate() and onResume() are pretty resource-intensive, they e.g. re-color bitmaps and dynamically set up the UI. This is unfortunately needed and can't be carried out asynchronously, as the user should see the final UI right from the beginning.Lulululuabourg
@Mike, thanks for trying it out on your end! I'll try to post some sample code. Could you add a Thread.sleep(1000) in your onCreate() and onResume()? From my understanding of the order in which onCreate(), onResume() and onAttachedToWindow() are called, this shouldn't make any difference (it should just delay the Activity from popping up, rather than leading to the keyguard being shown for 2 seconds)Lulululuabourg
Hey Nick, per your request I added the Thread.sleep(1000) call to both onCreate() and onResume(). On my Rezound & Ally, I get a black screen as it sleeps on both create & resume before displaying. However, on my Xperia Active (running 4.0.4), I hit a loop before the device wakes up, and sometimes I do indeed get the keyguard prior to the view! You're correct that onAttachedToWindow() fires in the lifecycle after the onCreate(), onStart(), and onResume() callbacks fire.Katydid
Nick, out of curiosity, which activity has the Window.addFlags() call? Your launcher activity, or another? And are you calling finish() anywhere?Katydid
Well you have mentioned your problem right here both my onCreate() and onResume() are pretty resource-intensive, they e.g. re-color bitmaps. You will get even ANR if you will try to block UI for more that ~5 sec. Instead you must do heavy lifting of bitmaps in background thread. Or prepare your bitmaps in your service, save them to sdcard and in your Activity just read them from sd card (of course from background thread). Reading bitmap won't take long so hopefully user won't notice anything.Fellatio
@M-WaJeEh, I should rephrase that, I'm applying a ColorFilter to an existing bitmap, so I'm not actually doing anything where loading the bitmap from storage would be quicker. When I launch the activity from a button (i.e. not from the background and without the keyguard showing), the UI-thread is blocked for maybe 1 second max.Lulululuabourg
@Mike, thanks for your tests! The activity that has the Window.addFlags() call is a "random" activity, i.e. not the launcher activity. I'm calling finish() after the user has "acknowledged" my activity, but not in any unusual places (like onPause(), for example). The activity awakes the screen at a number of points during the day, so my app's other activities are probably no longer in the activity stack in most instances.Lulululuabourg
I noticed that in my sample app, if I called finish() in the same activity that called addFlags(), the keyguard would not function properly. Moving the keyguard flag to another activity or removing the call to finish() fixed the issue entirely.Katydid
C
1

Have you tried UI Thread. Try to look at this good tutorial http://androidpartaker.wordpress.com/tag/ui-thread/. Hopre this helps you.

Chronologist answered 10/10, 2013 at 11:2 Comment(0)
K
0

try to add

android.permission.DISABLE_KEYGUARD permission to your manifest xml

source : link

Kharif answered 11/10, 2013 at 16:18 Comment(1)
Hi AT_AB, thanks for your answer. The WindowManager flag FLAG_DISMISS_KEYGUARD doesn't require this permission, as it only dismisses the "insecure" (swipe) keyguard. But the link you provided actually also mentions the problem I'm facing and it seems it doesn't happen when the "old" keyguard-methods are used.Lulululuabourg

© 2022 - 2024 — McMap. All rights reserved.