How can I detect a hardware-button-press from with

2019-07-21 02:13发布

问题:

I am developing a watch face for Android Wear and would like to use the hardware buttons of the watch (when available) to trigger some functions. The documentation says that this is possible for apps (https://developer.android.com/training/wearables/ui/multi-function).

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
  if (event.getRepeatCount() == 0) {
    if (keyCode == KeyEvent.KEYCODE_STEM_1) {
      // Do stuff
      return true;
    } else if (keyCode == KeyEvent.KEYCODE_STEM_2) {
      // Do stuff
      return true;
    } else if (keyCode == KeyEvent.KEYCODE_STEM_3) {
      // Do stuff
      return true;
    }
  }
  return super.onKeyDown(keyCode, event);
}

But my problem is that for the watch face I use the class:

    class MyWatchFace extends Gles2WatchFaceService

And onKeyDown() seems to be only available for a class that extends to AndroidApplication. Am I missing something, or is this just not possible for a watch face?

回答1:

You are correct. There's currently no way to capture events from the multi-function buttons in a WatchFaceService.