8

my app does something when the screen goes black, but I want it to only carry out that task if the screen was turned off "by itself", through a screen timeout - NOT when the user presses the power-button. Is there any way to distinguish between these two events?

ACTION_SCREEN_OFF obviously fires in both cases, and I haven't found any other intents that might match what I'm looking for.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Nick
  • 3,504
  • 2
  • 39
  • 78
  • What is it you're trying to do? There may be another action to listen for. – CaseyB May 04 '10 at 19:18
  • I'm trying to perform a simple calculation when the screen timed out, but not when the user presses the power-button to 'forcibly' turn the screen off. A "Timeout"-Action would be great, but I haven't found anything similar, yet. Is it possible to query whether the lock-screen is on (which, I believe, typically only happens when you press the power button, at least on the N1)? – Nick May 04 '10 at 21:36
  • Why do you want to do that in that situation only? If you explain *what* and *why*, perhaps someone can suggest you a better solution. – o0'. May 04 '10 at 22:29
  • Could this help: http://stackoverflow.com/a/8941006/969325 – Warpzit May 18 '13 at 19:42
  • Thanks, Warpzit, I'll check it out! – Nick May 18 '13 at 21:48

4 Answers4

4

At the lower level there's an event happening when the screen times out, take a look with "adb logcat" and you'll see it, dig from there.

berkus
  • 1,552
  • 12
  • 16
  • Thanks for your answer. I saw that, but thought that since it's not a public broadcast, I couldn't listen for it, but I'm sure I'll find something on that on google if you say that it is theoretically possible (although not with common means, I guess). Thanks! – Nick May 16 '10 at 10:19
1

Services can't directly handle button events. Perhaps you can have your Activity register a broadcast receiver to get notified when the screen is going off, and then have the Activity check the last key pressed to see if it was the power button--if it wasn't then you can send a broadcast to your service and have it run whatever you want.

Nelz11
  • 3,066
  • 1
  • 14
  • 20
1

That's a fun question :) Although not accurate, you can try sampling the time since the user last touch event and to the time your activity goes to pause - and see if it's fits the screen off timeout

Sean
  • 5,176
  • 2
  • 34
  • 50
0

Can you listen for a KEYCODE_POWER keyboard event? That might do what you want. (But the order in which that and ACTION_SCREEN_OFF arrive might not be guaranteed).

jimrandomh
  • 895
  • 8
  • 15
  • Hi, thanks for the answer! That's a good idea, although I just tried it and it doesn't work, it doesn't seem possible to catch that keycode: http://groups.google.com/group/android-developers/browse_thread/thread/8c858b7a8605791d/9120aa96dfb59d82?lnk=raot Also, I would need to register that key-event system-wide because I'm using a service, which is also not possible, I think :-/ But thanks for your help, it's a good idea, if it worked, it would have been perfect :-)! – Nick May 06 '10 at 21:18