tags:

views:

295

answers:

1

Hi,

The currently key events listener provided by android "onKeyUp", "onKeyDown" are associated to an activity , so they are notified of the key events if the activity's views are focused . What I want is to be able to increase/decrease the volume by pressing some of the device's key , without writing a listener in every activity , especially as my application has a lot of activities . Is there a way to write a "listener/piece of code" for key events that will be called always during the life of an application when a key is pressed, no matter what the user is doing at that time ?

A: 

Is there a way to write a "listener/piece of code" for key events that will be called always during the life of an application when a key is pressed, no matter what the user is doing at that time ?

Not for normal keys. Bear in mind that the volume buttons already adjust volume by default. There are a couple of buttons that have associated broadcast Intents (CAMERA, MEDIA) which you could set up a single listener for.

CommonsWare
well , I had in mind something like num key 4/ num key 6
Sorry -- you would have to override `onKeyDown()` in each of your activities. With inheritance, you might be able to actually have the implementation in only 1-3 base activity classes.
CommonsWare