android - 无法从 Google Fit API 获取每日步数
问题描述
我是 Google Fit API 的新手。我正在尝试从 API 获取每日步数,但没有运气。
我一直在查看官方文档和 Stack Overflow,但似乎 Google 在 2018 年对 API 进行了重大更改,因此大多数内容都已过时,包括 Google 自己的教程(发布于 2018 年)。一些更新了文档的地方提供了代码片段,因此我不知道如何使用它们。
这是我连接到 Google Fit 的代码:
FitnessOptions fitnessOptions = FitnessOptions.builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.build();
if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions)) {
GoogleSignIn.requestPermissions(
this, // your activity
GOOGLE_FIT_PERMISSIONS_REQUEST_CODE,
GoogleSignIn.getLastSignedInAccount(this),
fitnessOptions);
} else {
accessGoogleFit();
}
private void accessGoogleFit() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
long endTime = cal.getTimeInMillis();
cal.add(Calendar.YEAR, -1);
long startTime = cal.getTimeInMillis();
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.bucketByTime(1, TimeUnit.DAYS)
.build();
Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
.readData(readRequest)
.addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {
@Override
public void onSuccess(DataReadResponse dataReadResponse) {
Log.d(TAG, "onSuccess()");
Toast.makeText(getApplicationContext(),"onSuccess()",Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(),dataReadResponse.getBuckets().get(0).getDataSets()+"",Toast.LENGTH_LONG).show();
tvLastRun.setText(dataReadResponse.getBuckets().get(0).getDataSets()+"");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "onFailure()", e);
Toast.makeText(getApplicationContext(),"onFailure()",Toast.LENGTH_LONG).show();
}
})
.addOnCompleteListener(new OnCompleteListener<DataReadResponse>() {
@Override
public void onComplete(@NonNull Task<DataReadResponse> task) {
Log.d(TAG, "onComplete()");
Toast.makeText(getApplicationContext(),"onComplete()",Toast.LENGTH_LONG).show();
}
});
}
解决方案
08-27 13:10:39.488 1996-2723/com.google.android.gms.persistent W/InstanceID/Rpc: No response android.os.ConditionVariable@33657c3a
08-27 13:10:39.488 1996-2723/com.google.android.gms.persistent W/GLSUser: [AppCertManager] Failed to get security token.
java.io.IOException: TIMEOUT
at vpw.b(:com.google.android.gms@11947270:40)
at vpw.a(:com.google.android.gms@11947270:47)
at vpt.b(:com.google.android.gms@11947270:11)
at vpt.a(:com.google.android.gms@11947270:29)
at fvt.b(:com.google.android.gms@11947270:4)
at fvt.a(:com.google.android.gms@11947270:55)
at fvr.a(:com.google.android.gms@11947270:8)
at fvn.a(:com.google.android.gms@11947270:1)
at fvm.a(:com.google.android.gms@11947270:10)
at com.google.android.gms.auth.account.be.legacy.AuthCronChimeraService.b(:com.google.android.gms@11947270:7)
at faq.call(:com.google.android.gms@11947270:3)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at mwc.run(:com.google.android.gms@11947270:26)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at nbl.run(:com.google.android.gms@11947270)
at java.lang.Thread.run(Thread.java:818)
08-27 13:10:39.505 1996-2723/com.google.android.gms.persistent W/ConfigurationChimeraPro: Got null configs for com.google.`enter code here`android.gms.common_auth
08-27 13:10:39.524 1996-2723/com.google.android.gms.persistent I/Auth: [ReflectiveChannelBinder] Successfully bound channel!
推荐阅读
- javascript - 如何在无限滚动中清除以前的数据
- javascript - 如何在 javascript 的一个函数中使用 setTimeOut 和 setInterval
- c++ - 基本 C++ 问题(错误:预期表达式 '{' )
- javascript - 如何返回 React 容器的数值而不是对象?
- arrays - 如何将数组更改为列表
- reactjs - 从自定义 Hook 调用 graphql 查询返回数据
- python - E1101:模块“Milter”没有“CONTINUE”成员(无成员)
- java - RabbitMQ+SpringAMPQ:SimpleMessageListenerContainer中接收多个队列
- javascript - 使用 jquery 更改滑块句柄的位置
- javascript - @react-navigation/native 升级到 V5 SyntaxError