http://developer.android.com/training/wearables/data-layer/accessing.html

GoogleApiClient是一个用于整合所有谷歌服务的入口,想要连接数据层,需要构建一个对象.
GoogleApiClient提供了一个builder方法简化了构建对象的步骤.

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);
                    if (result.getErrorCode() == ConnectionResult.API_UNAVAILABLE) {
                        // The Android Wear app is not installed
                    }
                }
            })
        // Request access only to the Wearable API
        .addApi(Wearable.API)
        .build();

重要提示:当一个设备没有安装"Android Wear",那么GoogleApiClient则会返回连接失败,回调onConnectionFailed这个方法,错误码为ConnectionResult.API_UNAVAILABLE.此时若想要保持其他谷歌服务正常使用,应当将连接Wear API的GoogleApiClient对象与其他服务的对象独立开来.

在调用了GoogleApiClient的connect()方法后就会尝试着去连接服务,在连接成功后会回调onConnected(),在这个方法里,我们就可以调用数据层API.

相关文章:

  • 2021-06-13
  • 2022-02-01
  • 2022-12-23
  • 2022-12-23
  • 2022-01-20
  • 2021-11-17
猜你喜欢
  • 2021-07-10
  • 2021-10-24
  • 2022-02-23
  • 2021-09-18
  • 2021-10-17
  • 2021-05-05
  • 2021-04-21
相关资源
相似解决方案