我要在我的应用程序中计入Google适合步骤,因为我使用的是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);
任何帮助将不胜感激,我长期以来一直困扰于此问题,甚至没有从Google找到任何合适的教程。
答案 0 :(得分:1)
请参见FAQ,它从字面上解释了您要描述的行为。
这可能是最接近的;假设设备上安装了最新的Play服务:
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
之外,其他内容完全相同。或者,您可以记录自己的sessions-不仅是estimated_steps
,而且具有唯一的会话标识符。
答案 1 :(得分:0)
我在应用程序中使用了历史记录api,并将输出结果与Google FIT应用程序进行了比较,
val c = GregorianCalendar()
c.set(Calendar.HOUR_OF_DAY, 0) //anything 0 - 23
c.set(Calendar.MINUTE, 0)
c.set(Calendar.SECOND, 0)
var endTime = /*date.time + dayInMS*/(getCurrentUTCDate()*1000)
var startTime = (ConfigVariable.appLastUpdateTime*1000) //converting last updated data from second to MS
val readRequest = DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1 , TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build()
val pendingResult = Fitness.HistoryApi.readData(mGoogleApiClient,readRequest)
pendingResult.setResultCallback(this)