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

Accessing the Wearable Data Layer-数据层连接

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.

更多相关文章

  1. Android简易实战教程--第一话《最简单的计算器》
  2. Android中home键和back键区别实例分析
  3. 安卓开发中Android消息机制详解
  4. Android(安卓)UI开发第三十篇――使用Fragment构建灵活的桌面
  5. Unity与Android对比学习之生命周期方法
  6. Unity导出Android在高通骁龙800以上CPU概率性崩溃解决方法研究
  7. Android中AsyncTask的入门使用学习指南
  8. 再论android 2.2数据连接过程
  9. Android使用Service实现简单音乐播放实例

随机推荐

  1. Android(安卓)添加手势操作
  2. Android中activity跳转与Intent传值(重复)
  3. 9.Swift UIImagePickerController选取图
  4. Android加载GIF图片的两种方式
  5. Android不透明度16进制值
  6. android系统修改状态栏背景色以及文字颜
  7. Kotlin + Mvp + RxJava + Retrofit 心得
  8. Android(安卓)原生webview传递header前端
  9. Android(安卓)RecyclerView下拉刷新和上
  10. android处理处理触屏事件的流程