适用于Google Fit API的DataSet对象

时间:2014-11-10 13:44:11

标签: android api

目前使用Google Fit API并在使用Sessions API时遇到一些麻烦。我正在尝试为我的应用程序的锻炼插入会话,但文档有点令人困惑。

此代码段中是Google信息页面中的一个示例。您从何处获取DataSet对象和/或如何从现有数据创建它们?

    // Create a session with metadata about the activity.
    Session session = new Session.Builder()
            .setName(SAMPLE_SESSION_NAME)
            .setDescription("Long run around Shoreline Park")
            .setIdentifier("UniqueIdentifierHere")
            .setActivity(FitnessActivities.RUNNING)
            .setStartTime(startTime, TimeUnit.MILLISECONDS)
            .setEndTime(endTime, TimeUnit.MILLISECONDS)
            .build();

    // Build a session insert request
    SessionInsertRequest insertRequest = new SessionInsertRequest.Builder()
            .setSession(session)
            .addDataSet(runningDataSet)  //where does this come from???
            .build();

以下是Google Sessions API页面的链接(我正在使用此代码):

Google Fit API (Sessions API)

2 个答案:

答案 0 :(得分:2)

DataSet来自DataSource,因此您必须先创建一个DataSource,然后再创建DataSet.create(yourDataSource);

例如:

  DataSource dataSource = new DataSource.Builder()
                .setType(DataSource.TYPE_RAW)
                .setDataType(exerciseDataType)
                .setName(exercise.name)
                .setAppPackageName(this)
                .build();

  DataSet dataSet = DataSet.create(dataSource);

  ...

  dataSet.createDataPoint()....insert values :-)

答案 1 :(得分:0)

我现在正在做同样的事情。

如果您想使用Google健身API查看完整示例,可以访问以下链接:https://github.com/googlesamples/android-fit

您正在寻找的缺失代码是

    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    // Set a range of the run, using a start time of 10 minutes before this moment,
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.MINUTE, -10);
    long startTime = cal.getTimeInMillis();

    // Create a data source
    DataSource runningDataSource = new DataSource.Builder()
            .setAppPackageName(this.getPackageName())
            .setDataType(DataType.TYPE_SPEED)
            .setName(SAMPLE_SESSION_NAME + "-running speed")
            .setType(DataSource.TYPE_RAW)
            .build();

    float runSpeedMps = 10;
    // Create a data set of the running speeds to include in the session.
    DataSet runningDataSet = DataSet.create(runningDataSource);
    runningDataSet.add(
            runningDataSet.createDataPoint()
                    .setTimeInterval(startTime, startTime, TimeUnit.MILLISECONDS)
                    .setFloatValues(runSpeedMps)
    );