1

For an industrial app the customer has requested that the user be required to logon when the app is first stated, and again if the device "goes to sleep" meaning if he puts the device down and the screen goes off. This is so if the user has put the device down and walks away and someone else on the factory floor finds it later that person has to log-into the app under his own ID.

Anyway, I can force a logon for the onCreate event of the main screen Activity when the app first starts up but there doesn't seem to be an Activity life-cycle event for "waking up" after a display timeout. The onResume handler get called for all kinds of things, like just returning from another Activity in the same app, so that's too generic.

How can I detect when my app wakes up from a display time out?

N.B. - I don't care if I detect a display turn-on per se There are several discussions on Stack Overflow about detecting screen turn on ( Android - how to receive broadcast intents ACTION_SCREEN_ON/OFF?) where the consensus seemed to be that this hard and maybe even deliberately discouraged. I just want to know when my own app "wakes up". If detecting screen turn on is the only, or best, way to do this could someone please point me to some sample code?

Thanks in advance.

Community
  • 1
  • 1
user316117
  • 7,971
  • 20
  • 83
  • 158
  • Have you [searched stackoverflow](http://stackoverflow.com/questions/2577318/is-it-possible-to-write-an-android-broadcast-receiver-that-detects-when-the-phon)? – Steven Byle Apr 26 '13 at 20:17
  • http://stackoverflow.com/questions/9477922/android-broadcast-receiver-for-screen-on-and-screen-off – Scott W Apr 26 '13 at 20:17
  • Yes, I did do a search and I saw the above and a couple of other discussions about SCREEN_OFF (e.g., http://stackoverflow.com/questions/1588061/android-how-to-receive-broadcast-intents-action-screen-on-off) and the consensus seemed to be that this approached was either very hard or discouraged by Android. I'll update my question to reflect this. – user316117 Apr 26 '13 at 20:27

1 Answers1

2

How can I detect when my app wakes up from a display time out?

Use registerReceiver() (probably from onResume()) and unregisterReceiver() (probably from onPause()) to register for ACTION_USER_PRESENT. ACTION_USER_PRESENT differs from ACTION_SCREEN_ON in that ACTION_USER_PRESENT is not invoked until the user gets past the keyguard, whether that's swipe-to-unlock or a PIN or whatever.

My "probably" statements are because I have never tried your specific scenario. Normally, you'd use those methods in those lifecycle callbacks for dynamically registering a receiver, but for ACTION_USER_PRESENT, you might have to be more aggressive, using onCreate() and onDestroy() or something.

where the consensus seemed to be that this hard and maybe even deliberately discouraged

I have no idea how you came to that conclusion from that question and its answers.

I just want to know when my own app "wakes up".

There is no such concept. Apps don't "wake up". Devices "wake up".

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491