Android 在Google Fit中添加/读取体重和身高?安卓

Android 在Google Fit中添加/读取体重和身高?安卓,android,google-fit,Android,Google Fit,通过谷歌,我得到了插入数据类型的代码。键入步骤计数增量。但是如何使用Android插入TYPE\u HEIGHT和TYPE\u WEIGHT com.google.android.gms.common.api.Status insertStatus = Fitness.HistoryApi.insertData(mClient, dataSet) .await(1, TimeUnit.MINUTES

通过谷歌,我得到了插入
数据类型的代码。键入步骤计数增量。但是如何使用Android插入
TYPE\u HEIGHT和TYPE\u WEIGHT

 com.google.android.gms.common.api.Status insertStatus =
                    Fitness.HistoryApi.insertData(mClient, dataSet)
                            .await(1, TimeUnit.MINUTES);

要插入数据,需要为身高和体重创建新的DataSet对象

我创建了一个方法,以便获得一个数据集对象,该对象具有请求所需的参数

/**
 * This method creates a dataset object to be able to insert data in google fit
 * @param dataType DataType Fitness Data Type object
 * @param dataSourceType int Data Source Id. For example, DataSource.TYPE_RAW
 * @param values Object Values for the fitness data. They must be int or float
 * @param startTime long Time when the fitness activity started
 * @param endTime long Time when the fitness activity finished
 * @param timeUnit TimeUnit Time unit in which period is expressed
 * @return
 */
private DataSet createDataForRequest(DataType dataType, int dataSourceType, Object values,
                                     long startTime, long endTime, TimeUnit timeUnit) {
    DataSource dataSource = new DataSource.Builder()
            .setAppPackageName(mAppId)
            .setDataType(dataType)
            .setType(dataSourceType)
            .build();

    DataSet dataSet = DataSet.create(dataSource);
    DataPoint dataPoint = dataSet.createDataPoint().setTimeInterval(startTime, endTime, timeUnit);

    if (values instanceof Integer) {
        dataPoint = dataPoint.setIntValues((Integer)values);
    } else {
        dataPoint = dataPoint.setFloatValues((Float)values);
    }

    dataSet.add(dataPoint);

    return dataSet;
}
然后,您需要执行以下操作:

     DataSet weightDataSet = createDataForRequest(
           DataType.TYPE_WEIGHT,    // for height, it would be DataType.TYPE_HEIGHT
           DataSource.TYPE_RAW,
           value,                  // weight in kgs
           startTime,              // start time
           endTime,                // end time
           timeUnit                // Time Unit, for example, TimeUnit.MILLISECONDS
      );

    com.google.android.gms.common.api.Status weightInsertStatus =
            Fitness.HistoryApi.insertData(mClient, weightDataSet )
                    .await(1, TimeUnit.MINUTES);
如果你阅读,这是非常有帮助的。在那里,您可以阅读更多关于


我希望这有助于^ ^

您可以使用以下方法在更新的GoogleFit API中插入体重和身高:

private DataSet createDataForRequest(DataType dataType
,float dataSourceType
,int values
,long startTime, long endTime, TimeUnit timeUnit) {

    DataSource dataSource = new DataSource.Builder()
            .setAppPackageName(this)
            .setDataType(dataType)
            .setType(DataSource.TYPE_RAW)
            .build();

    DataSet dataSet = DataSet.create(dataSource);
    DataPoint dataPoint = dataSet.createDataPoint().setTimeInterval(startTime, endTime, timeUnit);

    float weight = Float.parseFloat(""+values);

    dataPoint = dataPoint.setFloatValues(weight);

    dataSet.add(dataPoint);

    return dataSet;
}

// to post data
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.DAY_OF_YEAR, -1);
long startTime = cal.getTimeInMillis();

DataSet weightDataSet = createDataForRequest(
        DataType.TYPE_WEIGHT,    // for height, it would be DataType.TYPE_HEIGHT
        DataSource.TYPE_RAW,
        56,                  // weight in kgs
        startTime,              // start time
        endTime,                // end time
        TimeUnit.MINUTES                // Time Unit, for example, TimeUnit.MILLISECONDS
);

com.google.android.gms.common.api.Status weightInsertStatus =
        Fitness.HistoryApi.insertData(mClient, weightDataSet )
                .await(1, TimeUnit.MINUTES);

// Before querying the data, check to see if the insertion succeeded.
if (!weightInsertStatus.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!");

// I'm getting : There was a problem inserting the dataset.
private void insertWeightHeight(Context context, DataType dataType, float value) {
        long startTime = Calendar.getInstance().getTimeInMillis();
        long endTime = startTime;
        DataSource dataSource = new DataSource.Builder()
                .setAppPackageName(getContext())
                .setDataType(dataType)
                .setType(DataSource.TYPE_RAW)
                .build();

        DataPoint dataPoint = DataPoint.builder(dataSource)
                .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
                .setFloatValues(value)
                .build();

        DataSet dataSet = DataSet.builder(dataSource)
                .add(dataPoint)
                .build();

        Fitness.getHistoryClient(context, GoogleSignIn.getLastSignedInAccount(context))
                .insertData(dataSet)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        System.out.println("success");
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        System.out.println("failed");
                    }
                });
    }

我也这样做了,你建议的方法如下:请看我的帖子:知道我为什么会这样吗:插入数据集时出现问题。现在试试,文档中有一个输入错误。顺便问一下,你有没有请求在Google Fit API中写入的权限?也许你没有在代码中添加正文权限。我建议阅读此响应:对于我来说,仅适用于dataPoint.getValue(Field.Field\u WEIGHT).setFloat((整数)值);
insertWeightHeight(getContext(), DataType.TYPE_WEIGHT, 70f);//weight in kg
insertWeightHeight(getContext(), DataType.TYPE_HEIGHT, 1.75f);//height in meter