Using the code below I have stopped the use of the volume buttons unless I am streaming audio (otherwise it annoyingly changes the ringer volume), but the 'Back' button isn't working.
Pressing 'back' should got to my phones desktop (or exit my app, like you would expect), but it isn't doing anything. If I open the menu, 'Back' will close the menu as it should, but I can't leave the app.
I have copied the code onto other activities within my app, if I open another activity within my app, because the 'Back' button isn't working, I can't go back to the main screen :)
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Suppress the use of the volume keys unless we are currently listening to the stream
if(keyCode==KeyEvent.KEYCODE_VOLUME_UP) {
if(StreamService.INT_PLAY_STATE==0){
return true;
}else{
return false;
}
}
if(keyCode==KeyEvent.KEYCODE_VOLUME_DOWN) {
if(StreamService.INT_PLAY_STATE==0){
return true;
}else{
return false;
}
}
return false;
Why is this happening?
Haven't tested, but I think you need to include an else where you call super.onKeyDown, ie:
if(keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
code
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
more code
} else {
super.onKeyDown(keyCode, event);
}
Otherwise, you're capturing all keycodes and returning false after checking the volume codes.
A simpler and more robust way to have the volume keys always control the Media volume is to insert this line into your Activity's onCreate():
setVolumeControlStream(AudioManager.STREAM_MUSIC);
Dude, just change the audio context on that activity to media volume:
http://developer.android.com/reference/android/media/AudioManager.html
EDIT:
private AudioManager audio;
Inside onCreate:
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Override onKeyDown:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return true;
default:
return false;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
//your code
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
//your code
return true;
} else {
super.onKeyDown(keyCode, event);
}
return true;
}
Related
On my Android TV if I launch Pandora in the background and let music play, then launch my app.
From my application if I press pause or next it receives these on my applicaiotn but also affects Pandora.
How can I prevent background applications from receiving these inputs.
I tried obtaining audio focus which effectively stops the music but if button input is made again music will resume.
Any ideas?
Here is my code capturing remote inputs.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((event.getSource() & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD) {
switch (keyCode) {
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
moveToNextImage(true);
break;
case KeyEvent.KEYCODE_MEDIA_REWIND:
moveToPreviousImage(true);
break;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
playPauseController();
break;
default:
break;
}
}
return super.onKeyDown(keyCode, event);
Found it! To prevent other applications from receiving remote inputs while my app is active, add the handled boolean.
Other option was gaining audio focus, but since I was not using audio it was not needed.
public boolean onKeyDown(int keyCode, KeyEvent event) {
boolean handled = false;
if ((event.getSource() & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD) {
switch (keyCode) {
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
moveToNextImage(true);
handled = true;
break;
case KeyEvent.KEYCODE_MEDIA_REWIND:
moveToPreviousImage(true);
handled = true;
break;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
playPauseController();
handled = true;
break;
default:
break;
}
}
return handled || super.onKeyDown(keyCode, event);
I have an app using firebase scanning to scan barcodes. I want to disable the use of volume up/down to open the scanner/camera. I also want the user to be able to still change the volume using those button.
I tried overwriting the onKeyDown event on my base activity but it doesn't fix my issue.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
return false;
} else {
return super.onKeyDown(keyCode, event);
}
}
Can anyone provide sample code for adjusting media volume using volume keys, and also it should display a volume alert progress while pressing the keys.
I think you want 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);
}
}
I'm trying to get my app to react to a long key press on volume down with the following code:
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
Log.w("myApp", "LONG PRESS");
}
return super.onKeyLongPress(keyCode, event);
}
However, it only spams a bunch of onKeyPress() events for volume down, and onKeyLongPress() never gets called. My intention is to leave the volume down and up "short" presses alone, and have my app react differently to the volume long press.
Can anybody point out what I'm missing?
You are almost there. You need to detect the same key event in the onKeyPress handler and start tracking it so that the long press can work. Here's the code you need:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
event.startTracking();
return true;
}
return super.onKeyDown(keyCode, event);
}
I currently have a mediaplayer streaming some mp3 files. What I would like to do is if the User presses the home button to exit the app, the music stops playing. how would I go about doing this?
In your app' onPause() or onDestroy() call mediaPlayer.stop().
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Log.d(this.getClass().getName(), "home button pressed"); }
return super.onKeyDown(keyCode, event); }
Something like this, except instead of log, do whatever you do in your implementation to kill the service/asynctask/thread handling your music.
Add this method to your class to override your home button
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME) {
media.stop();
finish();
//or complete end your application
//System.runFinalizersOnExit(true);
//System.exit(0);
return true;
}
return false;
}