问题
This is not a duplicate but a specific case to Android wear. I am trying to make Android wear app do something when certain gestures are performed. But I realized that the onKeyDown() event is not called at all. Any help or info I this regard is useful. Here is my simple code snippet.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gesture_ema);
Log.d(TAG, "Inside on create");
//Show prompt window on top always
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON +
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
//Prompt vibrator
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator != null) {
vibrator.vibrate(VIBRATION_PATTERN_INTENSE, -1);
}
// mTextView = (TextView) findViewById(R.id.text);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
Log.d(TAG, "Inside keydown");
switch (keyCode){
case KeyEvent.KEYCODE_NAVIGATE_NEXT:
Toast.makeText(this, "NAVIGATE_NEXT", Toast.LENGTH_LONG).show();
Log.d(TAG, "NAVIGATE_NEXT");
vibrator.cancel();
return true;
case KeyEvent.KEYCODE_NAVIGATE_PREVIOUS:
Toast.makeText(this, "NAVIGATE_PREVIOUS", Toast.LENGTH_LONG).show();
Log.d(TAG, "NAVIGATE_PREVIOUS");
vibrator.cancel();
return true;
}
return super.onKeyDown(keyCode, event);
}
EDIT
I first thought the problem was solved due to a permission I added. But that permission was deprecated and it had nothing to with this case. Then as someone in the comments suggested switching on the gestures, I realized there are places where the tilt gesture is actually used. One is to tilt and wake up the watch, which is controlled by Wear OS app. The second is in wear settings called enable gesture. This is a required step as it also goes through a calibration tutorial. Therefore, this thing doesn't have the permission as such and has to be done manually. Once I enabled that and completed the tutorial, the keydown event was getting called. Thanks everyone :-)
来源:https://stackoverflow.com/questions/50242174/android-wear-activity-onkeydown-is-not-getting-called