Friday, May 17, 2013

[android help] Taking over the volume key on Android


It's important to return true if you handled the event, but if you didn't handle the event, it's good to make sure that the event is still handled by the superclass. Here's some code from my app:



@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
changeColor(keyCode);

return true;
}

return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
// do nothing

return true;
}

return super.onKeyUp(keyCode, event);
}


In my case, the superclass call was necessary so that other hardware buttons on my device continued to work.



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...