【问题标题】:Get third party application (Sleep as Android) data from Google Fit从 Google Fit 获取第三方应用程序(Sleep as Android)数据
【发布时间】:2017-07-05 10:55:10
【问题描述】:

我目前正试图在我自己的 Android 应用程序中从 Google Fit 获取 Sleep as Android 数据。我从一篇文章中读到,Android 数据存储在 Google Fit 中,任何第三方应用程序都可以查看它们。为此,我使用历史会话。这是读取睡眠数据的示例代码:

public static DataReadRequest queryFitnessData() {
    // [START build_read_data_request]
    // Setting a start and end date using a range of 1 week before this moment.
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.WEEK_OF_YEAR, -1);
    long startTime = cal.getTimeInMillis();

    java.text.DateFormat dateFormat = getDateInstance();
    Log.i(TAG, "Range Start: " + dateFormat.format(startTime));
    Log.i(TAG, "Range End: " + dateFormat.format(endTime));

    DataReadRequest readRequest = new DataReadRequest.Builder()
            .read(DataType.TYPE_ACTIVITY_SEGMENT)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();
    // [END build_read_data_request]

    return readRequest;
}

并执行该查询:

DataReadRequest readRequest = queryFitnessData();
DataReadResult dataReadResult = Fitness.HistoryApi.readData(
        mClient, readRequest).await(1, TimeUnit.MINUTES);
printData(dataReadResult);

我的猜测是我错过了一些东西。我还阅读了 this 关于同一主题的帖子,但直到现在都没有成功。

[问题]

如何从 Google Fit 获取记录的睡眠活动健身数据?

[编辑]

这是 Google Fit 时间轴中 Sleep as Android 数据的屏幕截图

【问题讨论】:

  • 您是否正在将“Sleep as Android”中的数据与“Google Fit Platform”同步。能不能安装Google Fit app From Playstore看看能不能看到“Google Fit”应用中反映的睡眠数据
  • 是的,已经是这样了。让我通过将 Sleep as Android 中的数据屏幕截图添加到 Google Fit 来更新我的帖子。但是如何以编程方式获取这些数据?
  • 你能粘贴你得到的输出吗

标签: android google-fit


【解决方案1】:

终于,我明白了。感谢 Urbandroid 团队的 Martin Stava。

final SessionReadRequest.Builder sessionBuilder = new SessionReadRequest.Builder()

        .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
        .read(DataType.TYPE_ACTIVITY_SEGMENT)
        .readSessionsFromAllApps()
        .enableServerQueries();

final SessionReadRequest readRequest = sessionBuilder.build();

// Invoke the Sessions API to fetch the session with the query and wait for the result
// of the read request.
SessionReadResult sessionReadResult = SessionsApi.readSession(apiClient, readRequest).await(120, TimeUnit.SECONDS);

Status status = sessionReadResult.getStatus();

【讨论】:

  • 我找不到有关您使用此代码获得的响应的任何信息。你得到的信息格式和here一样吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2012-03-27
  • 1970-01-01
  • 2021-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多