我在我的Android应用程序中有一项服务用于计步。我获得每日和每周步骤的健身和历史api。一切都正常工作,直到突然步骤数始终为0!该应用程序似乎使用上面的代码连接到健身api
private void buildFitnessClient() {
mGoogleApiFitnessClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addApi(Fitness.HISTORY_API)
.addApi(Fitness.RECORDING_API)
.addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE))
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Google Fit connected.");
mTryingToConnect = false;
Log.d(TAG, "Notifying the UI that we're connected.");
notifyUiFitConnected();
}
@Override
public void onConnectionSuspended(int i) {
mTryingToConnect = false;
if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
Log.i(TAG, "Google Fit Connection lost. Cause: Network Lost.");
} else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.i(TAG, "Google Fit Connection lost. Reason: Service Disconnected");
}
}
}
)
.addOnConnectionFailedListener(
new GoogleApiClient.OnConnectionFailedListener() {
// Called whenever the API client fails to connect.
@Override
public void onConnectionFailed(ConnectionResult result) {
mTryingToConnect = false;
notifyUiFailedConnection(result);
}
}
)
.build();
}
虽然为了检索我使用此功能的每日步骤
private void getStepsToday() {
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
long endTime = cal.getTimeInMillis();
cal.setTime(now);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long startTime = cal.getTimeInMillis();
DebugLogger.debug("Value of startTime = "+startTime+" - endTime = "+endTime);
DebugLogger.debug("Test for getting steps "+getStepsCount(startTime,endTime));
final DataReadRequest readRequest = new DataReadRequest.Builder()
.read(DataType.TYPE_STEP_COUNT_DELTA)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
DataReadResult dataReadResult = Fitness.HistoryApi.readData(mGoogleApiFitnessClient, readRequest).await(1, TimeUnit.MINUTES);
DataSet stepData = dataReadResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA);
int totalSteps = 0;
DebugLogger.debug("GetStepsToday - Size in for loop "+stepData.getDataPoints().size());
for (DataPoint dp : stepData.getDataPoints()) {
for(Field field : dp.getDataType().getFields()) {
int steps = dp.getValue(field).asInt();
DebugLogger.debug("GoogleFit Service - debugging - steps count "+steps);
totalSteps += steps;
}
}
publishTodaysStepData(totalSteps, getGoalStep(goalEnabledDate));
}
正如我提到的一切正常,但突然stepData.getDataPoints()大小始终为0,返回0步。我为我的调试和发布密钥库创建了新的Oath密钥,我甚至更改了我的应用程序包名称,并在Android API控制台中重新创建了一个项目,但没有。有什么我应该更改权限或什么?我是否需要在我的应用程序中的任何位置声明我的密钥?
答案 0 :(得分:0)
检查设备上的Google Play服务版本,确保您的开发主机上有最新的Google Play服务客户端库。
apply plugin: 'com.android.application'
...
dependencies {
compile 'com.google.android.gms:play-services-fitness:9.0.1'
}