如何使用Android代码将卡路里数据插入googlefit应用程序

时间:2017-03-08 07:22:07

标签: android google-fit

以下代码用于将卡路里数据插入到googlefit应用程序中,就像这段代码完全适用于googlefit app中的步骤数据插入但不适用于卡路里并在googlefit app中显示不同的卡路里值。 请帮我解决这个问题。

Bundle argsBundle = getActivity().getArguments();
if(argsBundle != null) {
    String var_value=argsBundle.getStringExtra("data");
}

1 个答案:

答案 0 :(得分:0)

要在Google fit app上插入数据,您将使用Fitness History

  

首先创建一个数据集实例:

// Set a start and end time for our data, using a start time of 1 hour before this moment.
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.HOUR_OF_DAY, -1);
long startTime = cal.getTimeInMillis();

// Create a data source
DataSource dataSource = new DataSource.Builder()
        .setAppPackageName(this)
        .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
        .setStreamName(TAG + " - step count")
        .setType(DataSource.TYPE_RAW)
        .build();

// Create a data set
int stepCountDelta = 950;
DataSet dataSet = DataSet.create(dataSource);
// For each data point, specify a start time, end time, and the data value -- in this case,
// the number of new steps.
DataPoint dataPoint = dataSet.createDataPoint()
        .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS);
dataPoint.getValue(Field.FIELD_STEPS).setInt(stepCountDelta);
dataSet.add(dataPoint);
  

创建DataSet实例后,请使用HistoryApi.insertData   方法并同步等待或提供回调方法进行检查   插入的状态。

// Then, invoke the History API to insert the data and await the result, which is
// possible here because of the {@link AsyncTask}. Always include a timeout when calling
// await() to prevent hanging that can occur from the service being shutdown because
// of low memory or other conditions.
Log.i(TAG, "Inserting the dataset in the History API.");
com.google.android.gms.common.api.Status insertStatus =
        Fitness.HistoryApi.insertData(mClient, dataSet)
                .await(1, TimeUnit.MINUTES);

// Before querying the data, check to see if the insertion succeeded.
if (!insertStatus.isSuccess()) {
    Log.i(TAG, "There was a problem inserting the dataset.");
    return null;
}

// At this point, the data has been inserted and can be read.
Log.i(TAG, "Data insert was successful!");