是否可以添加一个简单的活动,例如我的Android应用程序的时间距离和卡路里骑自行车?我可以连接到我的Google健身帐户,但不知道如何添加像骑自行车这样的简单活动。
答案 0 :(得分:2)
是的,这是适用于我的代码..请查看
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");
}
答案 1 :(得分:0)