Android - Volume Buttons used in my application
You need to capture all actions:
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
How to override the behavior of the volume buttons in an Android application?
I imagine it looks something like this:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
// Do your thing
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
How can i use the the physical volume button on the phone to change the volume of applications?
Call the following right before you start playing music
setVolumeControlStream(AudioManager.STREAM_MUSIC);
and call the following after you finish
setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE);
This makes your volume rocker control all music streams and then resets it to default behavior (depends on what's happening).
Don't forget to call myMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)
before myMediaPlayer.prepare()
. Replace STREAM_MUSIC
in both calls with some other value if it fits the played sound better. This makes the music you play conform to the music volume set in system, which is now controlled by the volume rocker.
Source:
http://developer.android.com/training/managing-audio/volume-playback.html
http://developer.android.com/reference/android/media/MediaPlayer.html#setAudioStreamType(int)
Start My App On Volume key Pressed when app is closed
You should use broadcast receiver for volume button press.
Register receiver in manifest file
<receiver android:name="com.package.CallBroadcastReceiver" >
<intent-filter>
<action android:name="android.media.VOLUME_CHANGED_ACTION" />
</intent-filter>
</receiver>
and your broadcast activity look like this
public class CallBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent();
i.setClassName("com.package", "com.package.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Volume Control in android application
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.
Related Topics
Determine Addaction Click for Android Notifications
What Is Meaning of Boolean Value Returned from an Event-Handling Method in Android
How to Get Access Token After User Is Signed in from Gmail in Android
Linkedlist Put into Intent Extra Gets Recast to Arraylist When Retrieving in Next Activity
Gradle Dsl Method Not Found: 'Runproguard'
Display Badge on Top of Bottom Navigation Bar's Icon
Programmatically Getting the Gateway and Subnet Mask Details
Popupwindow - Dismiss When Clicked Outside
Programmatically Adding Tablerow to Tablelayout Not Working
Set the Layout Weight of a Textview Programmatically
New Intent() Starts New Instance with Android: Launchmode="Singletop"
Proguard Causing Runtimeexception (Unmarshalling Unknown Type Code) in Parcelable Class
What Is the Purpose of Android's <Merge> Tag in Xml Layouts
Programmatically Collapse or Expand Collapsingtoolbarlayout