Is it possible to create an Android Service that listens for hardware key presses?

As far as I know KeyEvents can only be handled by Activities as they are the interface to the user pressing the keys. Services run in the background and are not intended to react on user input. That’s also the reason of your compiler warning “onKeyDown is undefined for the type Service”. Service or any of it’s Superclasses don’t implement the KeyEvent.Callback interface. As a workaround you could register an Activity in your AndroidManifest.xml to react on certain system notifications such as android.intent.action.SCREEN_ON. When the power button is pressed to turn on the screen your activity could be started, initializing a service of some kind and go back to the background. If that’s what you intend to do. See Intent docs for possible Actions.

Hope that helped…

Leave a Comment