【发布时间】:2016-01-20 14:05:56
【问题描述】:
我已经对此进行了几天的研究。我只需要在我的应用程序中使用一个简单的 TextView 区域来显示今天的步骤。
我已经设法让身份验证与下面的代码一起工作。它弹出请求许可,并认为我选择了正确的。
但我不知道如何简单地获取步数信息。我希望这只是几行代码。任何帮助,将不胜感激。谢谢
编辑 1:我只需要获取步数。我可以弄清楚以后如何显示它。我也有 Toasts 来帮助我弄清楚发生了什么。
private void buildFitnessClient() {
if (mClient == null) {
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected!!!");
// Now you can make calls to the Fitness APIs.
Toast.makeText(getBaseContext(), "Connected!", Toast.LENGTH_LONG).show();
}
@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.");
Toast.makeText(getBaseContext(), "Connection lost. Cause: Network Lost.", Toast.LENGTH_LONG).show();
} 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());
Toast.makeText(getBaseContext(), "Google Play services connection failed. Cause: " +
result.toString(), Toast.LENGTH_LONG).show();
}
})
.build();
}
}
【问题讨论】:
-
您是否尝试过将
HistoryAPI用于 Google Fitness API。仅供参考:developers.google.com/fit/android/history
标签: java android api google-fit