Skip to content

Most visited

Recently visited

navigation

访问 Wearable Data Layer

要调用 Data Layer API,请创建一个 GoogleApiClient 实例,该实例是任何 Google Play Services API 的主要入口点。

:Wear 应用可以使用 Data Layer API 与手机应用通信,但不建议使用此 API 连接网络

GoogleApiClient 提供了一个生成器来简化客户端实例的创建。

:尽管下面的最简客户端可以满足基本需求,还请参阅访问 Google Play Services API,了解有关创建 GoogleApiClient、实现其回调以及处理错误情况的详细信息。

以下是一个最简客户端 GoogleApiClient

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(new ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle connectionHint) {
                    Log.d(TAG, "onConnected: " + connectionHint);
                    // Now you can use the Data Layer API
                }
                @Override
                public void onConnectionSuspended(int cause) {
                    Log.d(TAG, "onConnectionSuspended: " + cause);
                }
        })
        .addOnConnectionFailedListener(new OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.d(TAG, "onConnectionFailed: " + result);
                }
            })
        // Request access only to the Wearable API
        .addApi(Wearable.API)
        .build();

重要说明:如果要向 GoogleApiClient 添加多个 API,在未安装 Android Wear 应用的设备上可能会遇到客户端连接错误。要避免连接错误,请调用 addApiIfAvailable() 函数并传入 Wearable API,指示您的客户端应妥善处理缺少的 API。如需了解详细信息,请参阅访问 Wearable API

在您使用 Data Layer API 之前,请按启动连接中的说明,通过调用 connect() 函数,在您的客户端上启动连接。当系统为您的客户端调用 onConnected() 回调时,您就可以使用 Data Layer API 了。

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)