Google适合API - 无回调响应
Google Fit API - No Callback Response
我正在尝试集成来自 Google Fit API 的传感器 API。我正在构建 google 客户端,但没有关于任何状态的回调。
这是我的代码:
private void buildFitnessClient() {
if (mClient == null && checkPermissions()) {
Log.i(TAG, "Building Fitness Client");
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ))
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected!!!");
// Now you can make calls to the Fitness APIs.
findFitnessDataSources();
}
@Override
public void onConnectionSuspended(int i) {
// If your connection to the sensor gets lost at some point,
// you'll be able to determine the reason and react to it here.
if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
Log.i(TAG, "Connection lost. Cause: Network Lost.");
} else if (i
== GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.i(TAG,
"Connection lost. Reason: Service Disconnected");
}
}
}
)
.enableAutoManage(this, 0, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Google Play services connection failed. Cause: " +
result.toString());
Snackbar.make(
MainActivity.this.findViewById(R.id.main_activity_view),
"Exception while connecting to Google Play services: " +
result.getErrorMessage(),
Snackbar.LENGTH_INDEFINITE).show();
}
})
.build();
}
}
在LOGCAT中,我可以看到日志"Building Fitness Client"。在那之后,一切都是空白。没有错误。没有什么。
LOGCAT:
04-13 07:21:40.964 13226-13226/com.hbeat.hbeat I/BasicSensorsApi﹕ Ready
04-13 07:21:40.964 13226-13226/com.hbeat.hbeat I/BasicSensorsApi﹕ Building Fitness Client
04-13 07:21:41.154 13226-13226/com.hbeat.hbeat I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@42e7f528 time:435161647
04-13 07:21:41.154 13226-13226/com.hbeat.hbeat I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@42ddbcf0 time:435161647
04-13 07:21:42.154 13226-13243/com.hbeat.hbeat I/GMPM﹕ Tag Manager is not found and thus will not be used
04-13 07:21:43.674 13226-13226/com.hbeat.hbeat W/FragmentActivity﹕ Activity result delivered for unknown Fragment.
04-13 07:21:43.694 13226-13226/com.hbeat.hbeat I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@42e7f528 time:435164182
我在我的 KITKAT 设备上使用最新的播放服务 (8.7.03),并使用 8.4.0(修订版 29)构建,这在 SDK 管理器中显示为最新。
我已在 google 开发者控制台中为此应用程序正确添加了项目。
您需要调用 connect()
和 disconnect()
方法。当与 Google Play 服务的连接成功、失败或暂停时,接口接收回调以响应异步 connect()
方法。
mClient.connect();
可以在 Manually Managed Connections 中找到示例代码和更多信息。
Google 发布了新的 API,此问题已在其中解决。所以只需更新您的 build.gradle 文件:
compile 'com.google.android.gms:play-services-fitness:10.0.0'
我正在尝试集成来自 Google Fit API 的传感器 API。我正在构建 google 客户端,但没有关于任何状态的回调。
这是我的代码:
private void buildFitnessClient() {
if (mClient == null && checkPermissions()) {
Log.i(TAG, "Building Fitness Client");
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ))
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected!!!");
// Now you can make calls to the Fitness APIs.
findFitnessDataSources();
}
@Override
public void onConnectionSuspended(int i) {
// If your connection to the sensor gets lost at some point,
// you'll be able to determine the reason and react to it here.
if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
Log.i(TAG, "Connection lost. Cause: Network Lost.");
} else if (i
== GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.i(TAG,
"Connection lost. Reason: Service Disconnected");
}
}
}
)
.enableAutoManage(this, 0, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Google Play services connection failed. Cause: " +
result.toString());
Snackbar.make(
MainActivity.this.findViewById(R.id.main_activity_view),
"Exception while connecting to Google Play services: " +
result.getErrorMessage(),
Snackbar.LENGTH_INDEFINITE).show();
}
})
.build();
}
}
在LOGCAT中,我可以看到日志"Building Fitness Client"。在那之后,一切都是空白。没有错误。没有什么。 LOGCAT:
04-13 07:21:40.964 13226-13226/com.hbeat.hbeat I/BasicSensorsApi﹕ Ready
04-13 07:21:40.964 13226-13226/com.hbeat.hbeat I/BasicSensorsApi﹕ Building Fitness Client
04-13 07:21:41.154 13226-13226/com.hbeat.hbeat I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@42e7f528 time:435161647
04-13 07:21:41.154 13226-13226/com.hbeat.hbeat I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@42ddbcf0 time:435161647
04-13 07:21:42.154 13226-13243/com.hbeat.hbeat I/GMPM﹕ Tag Manager is not found and thus will not be used
04-13 07:21:43.674 13226-13226/com.hbeat.hbeat W/FragmentActivity﹕ Activity result delivered for unknown Fragment.
04-13 07:21:43.694 13226-13226/com.hbeat.hbeat I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@42e7f528 time:435164182
我在我的 KITKAT 设备上使用最新的播放服务 (8.7.03),并使用 8.4.0(修订版 29)构建,这在 SDK 管理器中显示为最新。
我已在 google 开发者控制台中为此应用程序正确添加了项目。
您需要调用 connect()
和 disconnect()
方法。当与 Google Play 服务的连接成功、失败或暂停时,接口接收回调以响应异步 connect()
方法。
mClient.connect();
可以在 Manually Managed Connections 中找到示例代码和更多信息。
Google 发布了新的 API,此问题已在其中解决。所以只需更新您的 build.gradle 文件:
compile 'com.google.android.gms:play-services-fitness:10.0.0'