1

我正在尝试创建和自定义数据类型并在其中添加价值。

我已经成功创建了 2 个字段,我正在回电。我的代码是

    // Subscribe to some data sources!
                            DataTypeCreateRequest  request = new DataTypeCreateRequest.Builder()
                                    // The prefix of your data type name must match your app's package name
                                    .setName("com.fitnessapi.data_type")
                                            // Add some custom fields, both int and float
                                    .addField("one", Field.FORMAT_FLOAT)
                                    .addField("two", Field.FORMAT_FLOAT)
                                    .addField(Field.FIELD_ACTIVITY)
                                    .build();



                            PendingResult<DataTypeResult> pendingResult = Fitness.ConfigApi.createCustomDataType(mClient, request);
                            request.


                            pendingResult.setResultCallback(
                                    new ResultCallback<DataTypeResult>() {
                                        @Override
                                        public void onResult(DataTypeResult dataTypeResult) {
                                            // Retrieve the created data type
                                            DataType customType = dataTypeResult.getDataType();
                                            System.out.println("one two" + customType.toString());


                                        }
                                    }
                            );

                            // [START auth_build_googleapiclient_ending]
                        }

我找不到任何方法来填充这两个字段中的值。

4

2 回答 2

5

这方面的文档对工作细节很清楚。下面是一个工作示例。

让我们从编写一些自定义数据开始,我将创建一个名为 flightcount 的字段(如用户在会话期间爬过的楼梯),它是一个 int。

我把这段代码放在我的 FitnessClient 触发 onConnected 回调的地方。请注意,我将数据类型存储为成员变量 mCustomType,稍后我们将需要它:

    DataTypeCreateRequest request = new DataTypeCreateRequest.Builder()
        // The prefix of your data type name must match your app's package name
        .setName("com.digitalconstruction.flightthepower.flights")
                // Add a custom field
        .addField("flightcount", Field.FORMAT_INT32)
                // Add some common fields
        .addField(Field.FIELD_ACTIVITY)
        .build();


PendingResult<DataTypeResult> pendingResult =
        Fitness.ConfigApi.createCustomDataType(mClient, request);


pendingResult.setResultCallback(
        new ResultCallback<DataTypeResult>() {
    @Override
    public void onResult(DataTypeResult dataTypeResult) {
        // Retrieve the created data type
        mCustomType = dataTypeResult.getDataType();
    }
}

);

插入数据集相当简单,但创建自定义数据集却不是,所以这里是示例代码。创建一个数据源,然后是数据集,然后是数据点。请注意数据点的创建,它与甚至无法编译的 Google 示例代码不同:

        // Create a data source
    DataSource climbDataSource = new DataSource.Builder()
            .setAppPackageName(this.getPackageName())
            .setDataType(mCustomType)
            .setName(SAMPLE_SESSION_NAME)
            .setType(DataSource.TYPE_RAW)
            .build();

    // Create a data set of the climb flight count to include in the session.
    DataSet climbDataSet = DataSet.create(climbDataSource);

    // Create a data point for a data source that provides
    DataPoint dataPoint = DataPoint.create(climbDataSource);

    // Set values for the data point
    // This data type has one custom fields (int) and a common field
    //tricky way to set single int
    dataPoint.getValue(mCustomType.getFields().get(0)).setInt(8);
    dataPoint.setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS);
    //tricky way to set activity, not at all how the non-working google sample code is set up
    FitnessActivities.setValue(dataPoint, FitnessActivities.STAIR_CLIMBING);

    climbDataSet.add(dataPoint);

最后我们要读取这个数据,方法dumpDataSet可以在这里找到。

//read custom data type: mCustomType
        Calendar cal = Calendar.getInstance();
        Date now = new Date();
        cal.setTime(now);
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.WEEK_OF_YEAR, -1);
        long startTime = cal.getTimeInMillis();

        final DataReadRequest readRequest = new DataReadRequest.Builder()
                .read(mCustomType)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();

        DataReadResult dataReadResult =
                Fitness.HistoryApi.readData(mClient, readRequest).await(1, TimeUnit.MINUTES);

        dumpDataSet(dataReadResult.getDataSet(mCustomType));

它有效,这是一些输出:

03-06 12:52:30.445 31506-31746/com.digitalconstruction.flightthepower I/MARK:字段:flightcount 值:8

03-06 12:52:30.445 31506-31746/com.digitalconstruction.flightthepower I/MARK:字段:活动值:77

希望这可以帮助。

于 2015-03-06T20:12:30.090 回答
1

我也遇到了这个问题。

医生说要使用 getVaule :

dataPoint.getValue(0).setInt(mField1IntValue);    

问题是 getVaule 方法需要一个字段。在玩弄它之后,我发现您可以通过以下方式获取字段列表:

dataSet.getDataType().getFields()

因此,要设置您的自定义 DateType 字段,我正在使用此方法。

dataPoint.getValue(dataSet.getDataType().getFields().get(0)).setInt(5);

不知道这是正确的,或者是一种愚蠢的做法,但它确实有效。

于 2015-01-15T08:09:44.643 回答