我在我的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步。我为我的调试和发布keystore创建了新的Oath密钥,我甚至更改了我的应用程序包名,并在Android控制台中重新创建了一个项目,但什么也没有。权限中有什么我应该更改的吗?我需要在我的应用程序中的任何地方声明我的钥匙吗?
https://stackoverflow.com/questions/37939171
复制相似问题