我正在尝试创建并custom datatype并在其中添加值。
我已经成功创建了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]
}
我无法找到任何方法来填充这2个字段中的值。
答案 0 :(得分:4)
docs对此有详细的了解。以下是一个工作示例。
让我们开始编写一些自定义数据,我将创建一个名为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可以找到here。
//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:Field:flightcount价值:8
03-06 12:52:30.445 31506-31746 / com.digitalconstruction.flightthepower I / MARK:领域:活动价值:77
希望这有帮助。
答案 1 :(得分:0)
我也遇到了这个问题。
该文档说要使用getVaule:
dataPoint.getValue(0).setInt(mField1IntValue);
问题是getVaule方法想要一个Field。 在玩完之后,我发现你可以通过以下方式获得Fields列表:
dataSet.getDataType().getFields()
因此要设置自定义DateType字段我正在使用此方法。
dataPoint.getValue(dataSet.getDataType().getFields().get(0)).setInt(5);
不知道这是对的,也可能是愚蠢的做法,但它有效。