1

我想在 google fit 中添加我的应用程序的锻炼。所以我现在尝试使用卷发锻炼,就像在官方示例中一样......我的代码是:

// Create a data source
        DataSource dataSource = new DataSource.Builder()
                .setAppPackageName(this)
                .setDataType(DataType.TYPE_WORKOUT_EXERCISE)
                .setName(getResources().getString(R.string.app_name))
                .setType(DataSource.TYPE_RAW)
                .build();

//        // Create a data set
        DataSet dataSet = DataSet.create(dataSource);
        DataPoint curls = DataPoint.create(dataSource);
        curls.setTimestamp(now.getTime(), TimeUnit.MILLISECONDS);
        curls.getValue(Field.FIELD_EXERCISE).setString(WorkoutExercises.BICEP_CURL);
        curls.getValue(Field.FIELD_DURATION).setInt(30000);
        curls.getValue(Field.FIELD_REPETITIONS).setInt(10);
        curls.getValue(Field.FIELD_RESISTANCE_TYPE).setInt(Field.RESISTANCE_TYPE_DUMBBELL);
        curls.getValue(Field.FIELD_RESISTANCE).setFloat(20.0f);
        dataSet.add(curls);

Fitness.HistoryApi.insertData(mClient, dataSet)
                .setResultCallback(new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {
                        if (status.isSuccess()) {
                            Log.i(TAG, "Data insert was successful!");
                        } else {
                            // The deletion will fail if the requesting app tries to delete data
                            // that it did not insert.
                            Log.i(TAG, "There was a problem inserting the dataset.");
                            //TODO store dataset to send it later or make this as a service
                        }
                    }
                });

问题是插入方法记录成功,但我看到 Google Fit 中没有添加任何数据。我尝试了其他类型的锻炼,比如木板,也达到了同样的效果。但是添加步数是有效的,就像在历史示例中的官方插入中一样,所以我想我错过了与类型锻炼相关的一些内容。有什么线索吗?

4

1 回答 1

3

据我所知,Google fit 不会显示锻炼练习。如果您想展示它,您可以在 TYPE_Activity_SAMPLE 或 TYPE _ACTIVITY_SEGMENT 数据类型中向 google fit 添加一个活动。WorkoutExercise 是不同的类,仅用于添加练习。如果您需要添加在 FitnessActivities 下定义的锻炼。您应该使用它,它还会在 google fit 应用程序中显示您的应用程序名称..

public void insertFitnessActivity(ExerciseBean bean) {

    /*
     * Distance Data source
     */ 
    DataSource distance = new DataSource.Builder()
            .setAppPackageName(context)
            .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);
    com.google.android.gms.common.api.Status distanceInsertStatus = Fitness.HistoryApi
            .insertData(fitnessClient, distanceDataSet).await(1, TimeUnit.MINUTES);

    /*
     * Steps Data source
     */
    DataSource steps = new DataSource.Builder()
            .setAppPackageName("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);
    com.google.android.gms.common.api.Status statusInsert = Fitness.HistoryApi
            .insertData(fitnessClient, stepsDataSet).await(1, TimeUnit.MINUTES);

    /*
     * Calory Data source
     */
    DataSource calory = new DataSource.Builder()
            .setAppPackageName("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.setFloatValues((float)bean.getEx_calories());
    calDataPoint.getValue(Field.FIELD_CALORIES).setFloat(
            (float) bean.getEx_calories());
    calDataSet.add(calDataPoint);

    com.google.android.gms.common.api.Status insertStatus = Fitness.HistoryApi
            .insertData(fitnessClient, calDataSet).await(1, TimeUnit.MINUTES);

    /*
     * Activity data source
     */
    DataSource acSource = new DataSource.Builder()
            .setAppPackageName(context)
            .setDataType(DataType.TYPE_ACTIVITY_SEGMENT)
            .setType(DataSource.TYPE_RAW).build();
    DataSet dataSet = DataSet.create(acSource);
    DataPoint dataPoint = DataPoint.create(acSource).setTimeInterval(
            bean.getStartTime(), bean.getEndTime(), TimeUnit.MILLISECONDS);
    dataPoint.getValue(Field.FIELD_ACTIVITY).setActivity(bean.getEx_Name());
    dataSet.add(dataPoint);

    com.google.android.gms.common.api.Status status = Fitness.HistoryApi
            .insertData(fitnessClient, dataSet).await(1, TimeUnit.MINUTES);
}
于 2015-12-21T06:31:12.703 回答