我正试图在我自己的Android应用程序中从Google Fit获取Sleep as Android数据。我在帖子中读到睡眠时Android数据存储在Google健身中,任何第三方应用都可以查看它们。为此,我使用历史会话。这是用于读取睡眠数据的示例代码:
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健身中获取睡眠活动的录制健身数据?
[编辑]
这是Google Fit的时间轴中的Sleep as Android数据的屏幕截图
答案 0 :(得分:0)
最后,我明白了。感谢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();