I need catch long press of volume button when phone is sleeping(screen off) and I know this code:
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
// to your stuff here
return true;
}
return super.onKeyLongPress(keyCode, event);
}
is not helpful(it works only in active intent
I’m curious about this too.
While this isn’t necessarily an answer, I have done some research on Services (to catch the volume press I’m guessing) and BroadcastReceivers (onReceive() would receive an intent for ACTION_SCREEN_OFF and probably set a flag for the screen being off). My thinking is starting the service when the screen turns off and kill it when the screen turns on, but I don’t know how to join onKeyLongPress and the service.
The sequence I see happening is this: Screen turns off -> BroadcastReceiver receives this command and starts the service to watch for volume press -> receive volume press and do your logic -> kill the service if the screen turns on.
I found a site that apparently handles screen off/on and has an example for an activity and a service but I can’t get something together yet:
http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/
One thing I’m worried about when using a service is battery life and processing time but that will be testing down the road. I will hopefully be able to mitigate that with killing the service when the screen turns on. I’ll try to keep this entry updated as I make progress.
Good luck!