【发布时间】:2018-03-06 04:43:41
【问题描述】:
我有 2 部手机使用 Gmail:
电话1:安装google fit app并将数据同步到网络[https://fit.google.com][1]
手机 2:未安装 google fit 应用,我使用 Google Fit Api 读取步数数据。 但它不能返回结果。
我的代码:
Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
.readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
.addOnSuccessListener { dataSet ->
val total = (if (dataSet.isEmpty)
0
else
dataSet.dataPoints[0].getValue(Field.FIELD_STEPS).asInt()).toLong()
Log.i(TAG, "Total steps: " + total)
txtStep.setText(total.toString())
}
.addOnFailureListener(
object : OnFailureListener {
override fun onFailure(e: Exception) {
Log.w(TAG, "There was a problem getting the step count.", e)
}
})
我必须在手机 2 上安装 google fit 应用吗? Google Fit API 是否只在本地读取数据?
我曾尝试使用 .enableServerQueries() 但它仍然无法返回数据。
val endTime = cal.timeInMillis
cal.add(Calendar.DAY_OF_YEAR, -1)
cal.set(Calendar.HOUR_OF_DAY, 23)
val startTime = cal.timeInMillis
var readRequest = DataReadRequest.Builder()
.read(DataType.TYPE_STEP_COUNT_DELTA)
.enableServerQueries()
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build()
Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
.readData(readRequest)
.addOnSuccessListener { dataReadResult ->
var total =0
if (dataReadResult.buckets.size > 0) {
for (bucket in dataReadResult.buckets) {
val dataSets = bucket.dataSets
for (dataSet in dataSets) {
for (dp in dataSet.dataPoints) {
for (field in dp.dataType.fields) {
total=total + dp.getValue(field).asInt()
}
}
}
}
} else if (dataReadResult.dataSets.size > 0) {
for (dataSet in dataReadResult.dataSets) {
for (dp in dataSet.dataPoints) {
for (field in dp.dataType.fields) {
total=total + dp.getValue(field).asInt()
}
}
}
}
txtStep.setText(total.toString())
}
.addOnFailureListener(
object : OnFailureListener {
override fun onFailure(e: Exception) {
Log.w(TAG, "There was a problem getting the step count.", e)
}
})
只有当我从Setting点击sync Google Fit Data时才能读取数据
【问题讨论】:
-
我不熟悉是否需要安装 Google fit 应用才能从其他设备获取数据,但您可以查看有关 Work with the Fitness History 的文档。它表示,“在记录数据之前创建订阅可以使您的应用程序与其他设备的数据同步,并允许在设备上被动记录数据。”同样在查询时,您必须在 DataReadRequest 中添加
.enableServerQueries()。希望这会有所帮助。 -
我在fit.google.com 有记录和数据。我曾尝试使用 enableServerQueries 但它仍然没有返回数据。
-
@DT 有同样的问题。你最终得到了这个工作吗?
-
@ciaranodc:不,我已经通过我们的服务器管理 Googlefit 数据。
标签: android google-fit