【发布时间】:2017-02-28 17:57:25
【问题描述】:
我是 Android Wearable 和 Google Fit 的新手。我正在通过在 Main Activity 中使用以下代码创建一个简单的 Android 应用程序来研究 Google Fit Recording API 的工作原理(这些代码取自 Google 的 Basic Recording API 示例):
@Override
protected void onCreate(Bundle savedInstanceState) {
...
buildFitnessClient();
}
private void buildFitnessClient() {
// Create the Google API Client
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.RECORDING_API)
.addScope(Fitness.SCOPE_LOCATION_READ_WRITE)
.addScope(Fitness.SCOPE_ACTIVITY_READ_WRITE)
.addScope(Fitness.SCOPE_BODY_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. What to do?
// Subscribe to some data sources!
subscribe();
}
@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 == ConnectionCallbacks.CAUSE_NETWORK_LOST) {
Log.i(TAG, "Connection lost. Cause: Network Lost.");
} else if (i == 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();
}
/**
* Subscribe to an available {@link DataType}. Subscriptions can exist across application
* instances (so data is recorded even after the application closes down). When creating
* a new subscription, it may already exist from a previous invocation of this app. If
* the subscription already exists, the method is a no-op. However, you can check this with
* a special success code.
*/
public void subscribe() {
// To create a subscription, invoke the Recording API. As soon as the subscription is
// active, fitness data will start recording.
// [START subscribe_to_datatype]
Fitness.RecordingApi.subscribe(mClient, DataType.TYPE_STEP_COUNT_DELTA)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
if (status.getStatusCode()
== FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
Log.i(TAG, "Existing subscription for activity detected.");
} else {
Log.i(TAG, "Successfully subscribed!");
}
} else {
Log.i(TAG, "There was a problem subscribing: " + status.getStatusMessage());
}
}
});
// [END subscribe_to_datatype]
}
我不确定录制 API 的工作原理。根据 Google Fit 文档,API 将自动负责跟踪步数(在这种情况下)并将步数存储到 Fitness Store 中。所以我希望应用程序能够工作的是启动应用程序,步行一段时间,然后在检查我的 Google Fit 帐户时,应该增加步数。
然而,这从未发生过。
如果我的理解不正确,请纠正我,或者如果是,请指出正确的方向以使此代码正常工作。
干杯
更新:一个多小时后,我的 Google Fit 数据显示步数从 3033(之前由 Fit 应用生成,后来为了安全测试该应用而被删除)增加到5011. 增加的数量非常令人困惑,因为我通过摇晃手机模拟了步骤,当然我没有考虑到 2000 次!我使用 Sensor API 实时显示步数,总共只有 200 步以下。
此外,我每 10-20 分钟手动检查一次数据,我确信数据更新需要 1 个多小时。根据 Google Fit 的文档,数据的存储(到 Fitness Store 中)是以“省电方式”自动完成的。然而,它并没有清楚地提到这是如何完成的,即频率如何。
如果有人能帮助我解决这些问题,那就太好了。任何帮助表示赞赏!
【问题讨论】:
标签: android google-fit