I am trying to override the volume buttons to act as Up/Down arrow keys (i.e. they should move focus around on all of my activities focus-able Views.)
To do so I am
I figured out eventually that you need to use the Instrumentation class to send the key events, and you have to do it off of the main thread for some reason. Here are the snippets that will send down and up:
new Thread(new Runnable() {
@Override
public void run() {
new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);
}
}).start();
If you use these insdie the overridden dispatchKeyEvent from the question it will correctly move focus around.