私のGoogleApiClient
接続は成功し、私@Override public void onDataPoint(DataPoint dataPoint) {…}
は一度だけ呼び出され、その後は二度と呼び出されません。これはService
、UI 内から Google Fit が承認された後にのみ開始される 内にあります (これは別のワームの缶です)。サービスにはGoogleApiClient
、正常に実行され、指定した間隔で呼び出される別のサービスがあります。
これが私が見たものですlogcat
:
mGoogleFitApiClient connected.
mGoogleFitApiClient listener registered.
mGoogleFitApiClient detected DataPoint: still (100.0% confidence)
その後、再び呼び出される@Override public void onConnectionSuspended(int i) {…}
ことはなく、呼び出されることもありません。
my からの関連コードは次のService
とおりです。
private void buildFitnessClient() {
mGoogleFitApiClient = new GoogleApiClient.Builder(this)
.useDefaultAccount()
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "mGoogleFitApiClient connected.");
setupPhysicalActivityListener();
}
@Override
public void onConnectionSuspended(int i) {
if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
Log.i(TAG, "mGoogleFitApiClient connection lost. Cause: network lost.");
} else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.i(TAG, "mGoogleFitApiClient connection lost. Reason: service disconnected");
}
}
}
)
.addOnConnectionFailedListener(
new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "mGoogleFitApiClient connection failed. Cause: " + result.toString());
}
}
)
.build();
}
@Override
public void onDataPoint(DataPoint dataPoint) {
String activityName = "";
float confidence = 0;
for (Field field : dataPoint.getDataType().getFields()) {
if (field.getName().equals("activity")) {
activityName = dataPoint.getValue(field).asActivity();
} else if (field.getName().equals("confidence")) {
confidence = dataPoint.getValue(field).asFloat();
}
}
lastActivityName = activityName;
lastActivityConfidence = confidence;
Log.i(TAG, "mGoogleFitApiClient detected DataPoint: " + activityName + " (" + confidence + "% confidence)");
}
private void setupPhysicalActivityListener() {
SensorRequest sensorRequest = new SensorRequest.Builder()
.setDataType(DataType.TYPE_ACTIVITY_SAMPLE)
.setSamplingRate(15, TimeUnit.SECONDS)
.setFastestRate(5, TimeUnit.SECONDS)
.setAccuracyMode(SensorRequest.ACCURACY_MODE_LOW)
.build();
Fitness.SensorsApi.add(mGoogleFitApiClient, sensorRequest, this)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
Log.i(TAG, "mGoogleFitApiClient listener registered.");
} else {
Log.i(TAG, "mGoogleFitApiClient listener not registered.");
}
}
});
}