gpt4 book ai didi

android - Google fit 以编程方式添加 Activity

转载 作者:搜寻专家 更新时间:2023-11-01 08:43:49 25 4
gpt4 key购买 nike

是否可以从我的 Android 应用程序添加一个简单的 Activity ,例如骑自行车与时间距离和卡路里?我可以连接到我的 Google Fit 帐户,但不知道如何添加像骑自行车这样的简单 Activity 。

最佳答案

是的,这是对我有用的代码..检查一下

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");
}

关于android - Google fit 以编程方式添加 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30306653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com