是否可以从我的 Android 应用程序中添加一个简单的活动,例如骑自行车的时间距离和卡路里?我可以连接到我的 Google fit 帐户,但不知道如何添加简单的活动,例如骑自行车。
问问题
1439 次
2 回答
4
是的,这是对我有用的代码.. 看看这个
private SessionInsertRequest insertFitnessSession(ExerciseBean bean) {
/*
* Distance Data source
*/
DataSource distance = new DataSource.Builder()
.setAppPackageName("com.package")
.setDataType(DataType.TYPE_DISTANCE_DELTA)
.setType(DataSource.TYPE_RAW).build();
DataSet distanceDataSet = DataSet.create(distance);
DataPoint distanceDataPoint = DataPoint.create(distance)
.setTimeInterval(bean.getStartTime(), bean.getEndTime(), TimeUnit.MILLISECONDS);
distanceDataPoint.getValue(Field.FIELD_DISTANCE).setFloat((float)bean.getEx_distance());
distanceDataSet.add(distanceDataPoint);
/*
* Steps Data source
*/
DataSource steps = new DataSource.Builder()
.setAppPackageName("com.package")
.setDataType(DataType.TYPE_STEP_COUNT_DELTA)
.setType(DataSource.TYPE_RAW).build();
DataSet stepsDataSet = DataSet.create(steps);
DataPoint stepsDataPoint = DataPoint.create(steps).setTimeInterval(
bean.getStartTime(), bean.getEndTime(), TimeUnit.MILLISECONDS);
stepsDataPoint.getValue(Field.FIELD_STEPS).setInt(bean.getSteps());
stepsDataSet.add(stepsDataPoint);
/*
* Calory Data source
*/
DataSource calory = new DataSource.Builder()
.setAppPackageName("com.package")
.setDataType(DataType.TYPE_CALORIES_EXPENDED)
.setType(DataSource.TYPE_RAW).build();
DataSet calDataSet = DataSet.create(calory);
DataPoint calDataPoint = DataPoint.create(calory).setTimeInterval(
bean.getStartTime(), bean.getEndTime(), TimeUnit.MILLISECONDS);
calDataPoint.getValue(Field.FIELD_CALORIES).setFloat((float)bean.getEx_calories());
calDataSet.add(calDataPoint);
/*
* Activity data source
*/
DataSource acSource = new DataSource.Builder()
.setAppPackageName("com.package")
.setDataType(DataType.TYPE_ACTIVITY_SEGMENT)
.setType(DataSource.TYPE_DERIVED).build();
DataSet dataSet = DataSet.create(acSource);
DataPoint dataPoint = DataPoint.create(acSource).setTimeInterval(bean.getStartTime(),bean.getEndTime(), TimeUnit.MILLISECONDS);
dataPoint.getValue(Field.FIELD_ACTIVITY).setActivity(FitnessActivities.BASKETBALL);
//dataPoint.getValue(Field.FIELD_DURATION).setInt(103434);// In milisecond
dataSet.add(dataPoint);
/*
* DataSource acSource = new DataSource.Builder()
* .setAppPackageName("com.package")
* .setDataType(DataType.AGGREGATE_ACTIVITY_SUMMARY)
* .setType(DataSource.TYPE_DERIVED).build(); DataSet dataSet =
* DataSet.create(acSource); DataPoint dataPoint =
* DataPoint.create(acSource).setTimeInterval(startTime, endTime,
* TimeUnit.MILLISECONDS);
* dataPoint.getValue(Field.FIELD_ACTIVITY).setActivity
* (FitnessActivities.CRICKET);
* dataPoint.getValue(Field.FIELD_DURATION).setInt(103434);// In
* milisecond dataSet.add(dataPoint);
*/
Session session = new Session.Builder().setName("SAMPLE_SESSION_NAME")
.setDescription("activity description")
.setIdentifier("UniqueIdentifierHere")
.setActivity(FitnessActivities.CRICKET)
.setStartTime(bean.getStartTime(), TimeUnit.MILLISECONDS)
.setEndTime(bean.getEndTime(), TimeUnit.MILLISECONDS)
.build();
// Build a session insert request
SessionInsertRequest insertRequest = new SessionInsertRequest.Builder()
.setSession(session)
.addDataSet(dataSet).addDataSet(calDataSet)
.addDataSet(stepsDataSet).addDataSet(distanceDataSet).build();
return insertRequest;
}
然后插入它。如果您不需要距离、卡路里和步数,您可以跳过这些数据集
SessionInsertRequest insertRequest = insertFitnessSession(bean);
com.google.android.gms.common.api.Status insertStatus = Fitness.SessionsApi
.insertSession(fitnessClient, insertRequest).await(1,
TimeUnit.MINUTES);
if (insertStatus.isSuccess()) {
Log.e("Activity", "Inserted");
} else {
Log.e("Activity", "inserted failed");
}
于 2015-10-14T15:51:35.460 回答