在我的应用程序中,我创建了一个记录步骤的订阅,这很好。问题是当屏幕关闭时,Google Fit不会计算任何步骤。这只发生在S4和S5上。它适用于两个S3,一个Nexus 7和一个S7。我甚至将我的步数与实际的Google健身应用进行了比较,后台步骤也没有在那里计算。
有点难过这个。不确定为什么它在某些设备上的表现会有所不同。
在onStart()中,我调用了buildFitnessClient()。
public void buildFitnessClient() {
// Create the Google API Client
mClient = new GoogleApiClient.Builder(getActivity())
.addApi(Fitness.RECORDING_API)
.addApi(Fitness.HISTORY_API)
.addApi(Fitness.SENSORS_API)
.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. What to do?
// Subscribe to some data sources!
subscribe();
readData();
}
@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.w(TAG, "Connection lost. Cause: Network Lost.");
} else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.w(TAG, "Connection lost. Reason: Service Disconnected");
}
}
}
)
.enableAutoManage(getActivity(), 0, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.w(TAG, "Google Play services connection failed. Cause: " +
result.toString());
}
})
.build();
subscribe();
//readData();
}
/**
* Record step data by requesting a subscription to background step data.
*/
public void subscribe() {
// To create a subscription, invoke the Recording API. As soon as the subscription is
// active, fitness data will start recording.
Fitness.RecordingApi.subscribe(mClient, DataType.TYPE_STEP_COUNT_CUMULATIVE)
.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.w(TAG, "There was a problem subscribing.");
}
}
});
}
public int startPedometer() {
listener = new OnDataPointListener() {
@Override
public void onDataPoint(DataPoint dataPoint) {
for (Field field : dataPoint.getDataType().getFields()) {
if (dataPoint.getDataType().equals(DataType.TYPE_STEP_COUNT_DELTA)) {
Value val = dataPoint.getValue(field);
Log.d("JUNE 12: ", "" + val);
presenter.setActiveFlag();
if (!presenter.checkPauseBool()) {
val.setInt(0);
presenter.putPauseBool();
}
mInitialNumberOfSteps += val.asInt();
//readData();
//_googleFit = new GoogleFit(getContext());
//int googleFitSteps = _googleFit.getSteps(_startTime);
if (getActivity() == null) {
return;
} else if (isAdded()) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
//_dailyStepTotal.setText("Steps: " + mInitialNumberOfSteps);
double cals = (_totalCalories - (mInitialNumberOfSteps * 0.044));
_remCals = (int) Math.round(cals);
if (isAdded()) {
_caloriesRemaining.setText(getResources().getString(R.string.crumble_calories_remaining, String.valueOf(_remCals)));
}
double eightyPercent = _totalCalories * 0.8;
eighty = (int) Math.round(eightyPercent);
double sixtyPercent = _totalCalories * 0.6;
sixty = (int) Math.round(sixtyPercent);
double fortyPercent = _totalCalories * 0.4;
forty = (int) Math.round(fortyPercent);
double twentyPercent = _totalCalories * 0.2;
twenty = (int) Math.round(twentyPercent);
if ((_remCals <= eighty && _remCals > sixty) && !_alreadyPlayed) {
presenter.onBeginCrumbleClick();
presenter.switchCrumbleImage(two);
_alreadyPlayed = true;
} else if ((_remCals <= sixty && _remCals > forty) && _alreadyPlayed) {
presenter.onBeginCrumbleClick();
presenter.switchCrumbleImage(three);
_alreadyPlayed = false;
} else if ((_remCals <= forty && _remCals > twenty) && !_alreadyPlayed) {
presenter.onBeginCrumbleClick();
presenter.switchCrumbleImage(four);
_alreadyPlayed = true;
} else if ((_remCals <= twenty && _remCals > 0) && _alreadyPlayed) {
presenter.onBeginCrumbleClick();
presenter.switchCrumbleImage(five);
_alreadyPlayed = false;
} else if ((_remCals == 0 || _remCals < 0) && !_alreadyPlayed) {
presenter.clearActiveFoodFlag();
presenter.addToHistory(_totalCalories);
presenter.saveSteps("0");
presenter.updateQueue();
fireworksAnimation();
_alreadyPlayed = true;
getActivity().finish();
}
}
}
);
}
}
}
}
};
SensorRequest req = new SensorRequest.Builder().setDataType(DataType.TYPE_STEP_COUNT_DELTA).setSamplingRate(1, TimeUnit.SECONDS).build();
result = Fitness.SensorsApi.add(mClient, req, listener);
//Log.d("Steps: ", "" + result);
return mInitialNumberOfSteps;
}