android - 从 History api 接收的步骤数据与 google fit 不匹配
问题描述
我希望在我的应用程序中计算 google fit 步数,因为我使用的是由 google 提供的 History api。我发现从历史 api 接收的步骤与 google fit 不匹配,即使我使用了 google 提供的相同代码。下面是我的代码。
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long startTime = cal.getTimeInMillis();
DataSource ESTIMATED_STEP_DELTAS = new DataSource.Builder()
.setDataType(DataType.TYPE_STEP_COUNT_DELTA)
.setType(DataSource.TYPE_DERIVED)
.setStreamName("estimated_steps")
.setAppPackageName("com.google.android.gms")
.build();
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
.aggregate(DataType.TYPE_DISTANCE_DELTA, DataType.AGGREGATE_DISTANCE_DELTA)
.aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
Task<DataReadResponse> result = Fitness.getHistoryClient(getApplicationContext(),
GoogleSignIn.getLastSignedInAccount(getApplicationContext())).readData(readRequest);
任何帮助将不胜感激,我长期以来一直困扰着这个问题,甚至没有从谷歌找到任何合适的教程。
解决方案
请参阅常见问题解答,它从字面上解释了您正在描述的行为。
这可能是最接近的;假设设备上安装了最新的播放服务:
DataSource ESTIMATED_STEP_DELTAS = new DataSource.Builder()
.setDataType(DataType.TYPE_STEP_COUNT_DELTA)
.setType(DataSource.TYPE_DERIVED)
.setStreamName("estimated_steps")
.setAppPackageName("com.google.android.gms")
.build();
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(ESTIMATED_STEP_DELTAS, DataType.AGGREGATE_STEP_COUNT_DELTA)
.aggregate(DataType.TYPE_DISTANCE_DELTA, DataType.AGGREGATE_DISTANCE_DELTA)
.aggregate(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED)
.aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
它完全一样,除了TYPE_CALORIES_EXPENDED
。或者,您可以记录自己的会话- 不仅是estimated_steps
,而且具有唯一的会话标识符。
推荐阅读
- php - 在 php 标头中使用查询
- python - 创建可在远程网络上访问的 TCP 服务器
- javascript - 如何使用具有一个功能的 Javascript 创建多选项卡?
- django - 方法不允许(GET):和一键错误
- mongodb - $Lookup 在具有更多属性的数组中 MongoDB
- python - 我的代码混淆了正则表达式的输入文件名
- excel - 当我尝试在两个不同的工作簿之间工作时出现错误 438。具体来说,我想从一个打印行数
- android - Firestore RecyclerView app's crashing when run
- r - 在 R 中将数据框旋转两次,然后恢复到原始形状
- c++ - 为什么我的程序卡在构造函数上?