3

I have a service that detects when the display of my Android phone is turned on or off. When turned off, the service calls an activity that uses dispatchKeyEvent to detect when the volume up button has been pressed. Unfortunately, apparently the activity can't do this when the screen is off. (See this post.)

I've noticed, though, that something at some level is detecting that event, since the following LogCat message appears when I press the volume-up button when the screen is off: "CatService: Return current sInstance". The message seems to be device specific, since on a different device something different appears in the log, but I'm really only concerned about the first device.

I've done some research into CatService, but haven't found much and can't figure out how I might be able to use it, or whatever is generating the log message, to detect the volume up button press. I'd appreciate any light that anyone can shed on this.

hBrent
  • 1,696
  • 1
  • 17
  • 38
  • 1
    If you are running logcat, you likely have the USB cable connected and are preventing the phone's processor from going to sleep. The need to let it sleep when running on battery is one of the major impediments to the idea. – Chris Stratton Jun 30 '12 at 22:27
  • https://stackoverflow.com/a/35168869/1293492 – Tamás Bolvári Apr 07 '20 at 19:18

1 Answers1

4

The only way to keep detecting things like this when the screen is off is to acquire a WakeLock that will allow the screen to turn off, and still let your app function. However, this drains the battery life quite a bit, and should only be used when absolutely necessary.

In this case, you will need a PARTIAL_WAKE_LOCK.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • Thanks, Raghav and Chris. The reason for my question is that I very recently upgraded to ICS, and in doing so lost the ability to wake my phone/turn on the screen using the volume buttons (apparently built into 2.3 or the version of Sense I had before (that got upgraded, too)) and was hoping to create something to restore that functionality (my power button is pretty hard to press). Based on what you're telling me, whatever was detecting the volume button presses before operated in a fundamentally different way from anything I can do, since it was baked in at a lower level. – hBrent Jul 01 '12 at 18:51
  • If it was a functionality in the Stock ROM, then it was likely added at the firmware level, much like the handling on the home button. – Raghav Sood Jul 01 '12 at 18:52