我正在使用Google Fit进行录制。不要清除Google健身日数据。 2应用程序中的按钮。第一个开始记录步骤,第二个代表停止记录
我的开始提示代码
Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount(this))
.subscribe(DataType.TYPE_STEP_COUNT_DELTA)
.addOnCompleteListener(
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
} else {
}
}
});
//
Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount(this))
.subscribe(DataType.TYPE_DISTANCE_DELTA)
.addOnCompleteListener(
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
} else {
}
}
});
//
Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount(this))
.subscribe(DataType.TYPE_CALORIES_EXPENDED)
.addOnCompleteListener(
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
} else {
}
}
});
删除数据的代码不起作用
public void ClearHistory(){
Log.i(TAG, "Deleting today's step count data");
// [START delete_dataset]
// Set a start and end time for our data, using a start time of 1 day before this moment.
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.DAY_OF_MONTH, -20);
long startTime = cal.getTimeInMillis();
// Create a delete request object, providing a data type and a time interval
DataDeleteRequest request = new DataDeleteRequest.Builder()
.setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
.addDataType(DataType.TYPE_STEP_COUNT_DELTA)
.build();
// Invoke the History API with the Google API client object and delete request, and then
// specify a callback that will check the result.
Fitness.HistoryApi.deleteData(mClient, request)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
Log.i(TAG, "Successfully deleted today's step count data");
Toast.makeText(p_context,"ok",Toast.LENGTH_LONG).show();
Toast.makeText(p_context,status.getStatus().toString(),Toast.LENGTH_LONG).show();
} else {
// The deletion will fail if the requesting app tries to delete data
// that it did not insert.
Log.i(TAG, "Failed to delete today's step count data");
}
}
});
}
获取数据
Fitness.getHistoryClient(p_context, GoogleSignIn.getLastSignedInAccount(p_context))
.readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
.addOnSuccessListener(
new OnSuccessListener<DataSet>() {
@Override
public void onSuccess(DataSet dataSet) {
long total =
dataSet.isEmpty()
? 0
: dataSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
//String Calley = dataSet.getDataPoints().get(0).getValue(Field.FIELD_CALORIES).toString();
/*
long distance =
dataSet.isEmpty()
? ""
: dataSet.getDataPoints().get(0).getValue(Field.FIELD_DISTANCE.getFormat());
*/
//
Log.i("countstep", "Total steps: " + total);
//CalleryStart = Double.parseDouble(String.valueOf(Calley));
//distancestart = Double.parseDouble(String.valueOf(distance));
//Toast.makeText(getApplicationContext(), Double.toString( total),Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w("errorcountstep", "There was a problem getting the step count.", e);
}
});
此Google Fit Api的代码不起作用我正在使用Google Fit版本16.0.1请勿输入任何字词。获得数据成功完成工作并记录步骤...。没有有效的删除历史记录。我正在使用Google Fit API文档