Android Google Fitness startSession中可能存在竞争条件或程序员错误
我试图在我的裁判表中记录体育活动期间的Google Fit训练。我在周期开始时开始一个会话,然后在周期结束时停止它 当我使用以下代码开始健身课程时,有时会遇到一个有趣的异常:Android Google Fitness startSession中可能存在竞争条件或程序员错误,android,google-fit,google-fit-sdk,Android,Google Fit,Google Fit Sdk,我试图在我的裁判表中记录体育活动期间的Google Fit训练。我在周期开始时开始一个会话,然后在周期结束时停止它 当我使用以下代码开始健身课程时,有时会遇到一个有趣的异常: private Session startFitSession(final Game currentGame) { final Session fitSession; try { String sessionBaseName = currentGame
private Session startFitSession(final Game currentGame) {
final Session fitSession;
try {
String sessionBaseName = currentGame.getGameTitle();
if (sessionBaseName.isEmpty()) sessionBaseName = currentGame.getGameLocation();
if (sessionBaseName.isEmpty()) sessionBaseName = RefWatchUtil.timeMsToString(currentGame.getActualStartMs(),RefWatchUtil.dateTimeFormatStart);
final String sessionName = sessionBaseName + ": "
+ String.format(getResources().getString(R.string.fitness_period_label),mCurrentPeriod);
//use this to try to avoid error message about creating a session in the future
final long startTime = System.currentTimeMillis()-TimeUnit.SECONDS.toMillis(10);
fitSession = new Session.Builder()
.setName(sessionName)
.setIdentifier(sessionName)
.setDescription(mCurrentGame.getGameDescription())
.setStartTime(startTime, TimeUnit.MILLISECONDS)
.setActivity(FitnessActivities.RUNNING_JOGGING)
.build();
Fitness.SessionsApi.startSession(mGoogleApiClient, fitSession)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if (status.isSuccess()) {
Log.i(TAG, "Successfully started Session " + sessionName);
} else {
Log.i(TAG, "There was a problem starting the session " + sessionName
+ ": " + status.getStatusMessage());
}
}
});
} catch (RuntimeException e){
Log.i(TAG, "There was a runtime exception starting the session: " + e.getLocalizedMessage());
return null;
}
return fitSession;
}
同样的错误
现在我从开始时间中减去任意10秒,这似乎解决了问题
但是(a)是否有更好的方法进行此调用(afaict,您不能使用0参数调用setStartTime以获取“当前时间”)和(b)这是生成器类型调用的常见模式?嘿,您找到相同的解决方案了吗?嘿,您找到相同的解决方案了吗?
...
final long startTime = System.currentTimeMillis();
fitSession = new Session.Builder()
.setName(sessionName)
.setIdentifier(sessionName)
.setDescription(mCurrentGame.getGameDescription())
.setStartTime(startTime, TimeUnit.MILLISECONDS)
...