tags:

views:

598

answers:

1

I'd like to know how to control my application's volume from the volume keys (contrary to my belief , I've read they control only the ringer volume). Should I overwrite the onKey Down/Up?

Or is there other way to accomplish this? I'm asking because if I overwrite the upper mentioned function for an activity, then the functions will receive the event only if a view associated with the this activity has the focus, and I'm looking for something "Globaly" ( to work no matter what activity is running now)

+4  A: 

There was another question from a long time ago that asked the same thing. Essentially the answer is: don't override the onKeyDown and onKeyUp buttons. It's much better to simply use this one line setVolumeControlStream(AudioManager.STREAM_MUSIC); in your onCreate() method. That tells the OS that the volume buttons should affect the "media" volume when your application is visible, and that's the volume it uses for your application.

As for controlling the Media volume no matter what app is visible, I'm not sure that can be done - or if it could, whether that would be a good thing.

Steve H
The thing is that I'm interested to function only when activities of my application are running, I don't have any desire to make it work ,if an activity from other app is at the top of the activity stack
rantravee
Thanks ! I guess it could work for me ! Is there any suggestion in what activity should this line be added ?
rantravee
Put it in your onCreate() method of any activity that's going to be making sound.
Steve H
By the way, you should look through the questions you've asked, and mark answers as "accepted" if the answers helped you. This is a nice thing to do for the people who helped you as it gives them 'reputation' - the number shown below our usernames. Read the FAQ for more detail.
Steve H
By the way , If I catch the event of pressing down the volume keys in onKeyDown , and return super.onKeyDown , do I distract the android sound system ? ....I just want to be informed of such a press without any other interaction with the sound
rantravee
Hmm. I think that would work. Best way to find out is just to test it. Put the code in with just a `Log` line and super.onKeyDown, then see what happens. If the Android floating box with the current volume appears and LogCat shows your message, then it would seem that your idea worked.
Steve H