1

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 :-)

7
  • onKeyDown is triggered when a certain button is pressed. However, your usecase is implying that you want to use Motion Sensors or gyroscope for Android Wear when you said "gestures". May 9, 2018 at 11:14
  • You are right. But Android wear already has inbuilt gestures that can be used. At least according to their official documentation, my code makes sense: developer.android.com/training/wearables/ui/wrist-gestures May 9, 2018 at 15:48
  • I know this might sound stupid... Have you turned on "wrist gestures" in settings? Does it work in other apps?
    – TofferJ
    May 9, 2018 at 16:39
  • Just tried. But I think that is like a global setting to wake up the watch with a gesture. What I am trying to do is use it in my own activity to do something. But the keydown event itself is not getting called. May 9, 2018 at 16:54
  • Got it working with the manifest permissions May 9, 2018 at 17:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.