I'm trying to reprogram the functionality of the Volume Up and Volume down keys. My approach uses a floating view which intercepts the key events(similar to `chat heads` app). The view flag that I'm using is: LayoutParams.FLAG_NOT_TOUCH_MODAL
, but unfortunately this blocks the other events. For example, if I try to use a messenger app, the keyboard doesn't appear and the backButton is blocked. This is due to the fact that my view has the focus.
Following the advice doesn't work for my case. On github I have the complete code.
I'm struggling with the view flags part. Do you have any idea for me? Do I have to use a different approach in order to have a view which responds to key events and doesn't interfere with the rest of the apps?
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int keyCode = event.getKeyCode();
int action = event.getAction();
Log.d(TAG, MotionEvent.actionToString(action)
+ ", " + KeyEvent.keyCodeToString(keyCode));
if (event != null && mListener != null) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
mListener.onBackButtonPressed();
return true;
}
if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
Toast.makeText(getContext(), "volume up", Toast.LENGTH_SHORT).show();
return true;
}
if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
Toast.makeText(getContext(), "volume down", Toast.LENGTH_SHORT).show();
return true;
}
}
return super.dispatchKeyEvent(event);
}