6

Google によって、挿入するこのコードを取得しましたDataType.TYPE_STEP_COUNT_DELTATYPE_HEIGHT AND TYPE_WEIGHTしかし、 Androidを使用して挿入する方法

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

3 に答える 3

0
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.
于 2014-11-14T13:28:37.177 に答える