コンテンツに移動

よくアクセスするページ

最近アクセスしたページ

navigation

Multi-function Buttons on Wear

In addition to the power (stem) button, Wear OS by Google supports extra physical buttons known as multi-function (MF) buttons. The Wearable Support Library provides APIs to determine information about the available MF buttons on a device.

This lesson focuses on how you can retrieve information about the available MF buttons on a device and process KeyEvent objects.

Refer to the following related resources:

Handling Key Events

If your app needs to handle a multi-function button press, it can do so through the standard key press event codes in the Android framework. Key codes typically correspond one-to-one with individual physical buttons on a device.

The available set of possible button keycodes that your app can handle is: KEYCODE_STEM_1, KEYCODE_STEM_2, KEYCODE_STEM_3. Your app can receive these key codes and convert them to specific in-app actions.

To handle a button press, implement the onKeyDown() method.

For example, this implementation responds to some button press to control actions in an app:

@Override
// Activity
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);
}

Finding Available Buttons

To find out which buttons are available for use, you can use the WearableButtons.getButtonInfo() and WearableButtons.getButtonCount() methods available in the Wearable Support Library. A simple way to find if there are secondary buttons available is to check the return value of WearableButtons.getButtonCount(). If it's greater than 1, then there are multi-function buttons available for use.

Keycodes for button presses

Each button is mapped to an int constant from the KeyEvent class, as shown in the following table:

Button KeyEvent
MF button 1 KEYCODE_STEM_1
MF button 2 KEYCODE_STEM_2
MF button 3 KEYCODE_STEM_3

The following example code shows how to get the available button count:

int count = WearableButtons.getButtonCount(mContext);

if (count > 1) {
  // There are multi-function buttons available
}

WearableButtons.ButtonInfo buttonInfo =
  WearableButtons.getButtonInfo(mActivity, KeyEvent.KEYCODE_STEM_1);

if (buttonInfo == null) {
  // KEYCODE_STEM_1 is unavailable
} else {
  // KEYCODE_STEM_1 is present on the device
}

Determining the Button Positions

The Wearable Support Library provides two APIs that describes the location of a button:

Note: We recommend that you refrain from using textual descriptors when describing buttons and their functions and instead use visual indicators. However, there may be some cases where describing a button makes more sense.

The above methods were designed for simple descriptions. If these APIs do not suit your app’s needs, you can also use the WearableButtons.getButtonInfo() API to get the location of the button on the screen and handle it in a more cusomized way. For more information on the APIs, see the Wear API Reference.

このサイトでは、ユーザーが選択したサイトの言語と表示設定を保存する目的で Cookie を使用しています。

Android デベロッパー向けの最新情報やヒントを入手して、Google Play での成功を手に入れましょう。

* 必須

送信しました

WeChat で Google Developers をフォローする

このサイトをで表示しますか?

ページの表示言語としてを選択しましたが、このサイトの言語はに設定されています。

言語設定を変更してこのサイトをで表示しますか?言語設定を変更する場合は、各ページの下にある言語メニューを使用してください。

このクラスには、API レベル 以上が必要です。

API レベル が選択されているため、このドキュメントは非表示になっています。左のナビゲーションの上にあるセレクタを使って、ドキュメントの API レベルを変更できます。

アプリに必要な API レベルを指定する方法について、詳しくは異なるプラットフォーム バージョンのサポートをご覧ください。

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)