我有2部使用Gmail的电话:电话1:安装Google Fit应用并将数据同步到Web [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)
}
})
我必须在Phone 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)
}
})
仅当我从sync Google Fit Data
单击Setting
时才能读取数据>
[我有2部使用Gmail的电话:电话1:安装google fit应用并将数据同步到网络[https://fit.google.com] [1]电话2:未安装google fit应用,我使用的是Google Fit Api读取步骤数据。但是...
如果您不想拥有Google Fit应用,则可以使用getRecordingClient