我在健身应用程序中遇到问题,1小时时间任务运行缓慢或有时它运行得太快,如果它增加2秒simantaneously请有人建议稳定的解决方案..我保存用户的历史,当我得到新的位置任何帮助是赞赏
public void reStartTimerTask(final boolean onCreate) {
if (Validator.isNull(timer)) {
if (preferences.isGps()) {
IntentFilter intentFilter = new IntentFilter(Util.LOCAL_RECEIVER);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
workoutReceiver = new WorkoutReceiver();
registerReceiver(workoutReceiver, intentFilter);
if (!onCreate) {
if (!Util.isGPSEnabled(this)) {
displayGPSAlert();
}
if (!preferences.isTryOut()) {
Request.getRequest().sendRequest(Request.GET_ALL_TRACE_USER, this, this, RequestParameterBuilder.buildMapForUserId(this));
}
}
}
timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
if (preferences.isGps()) {
WorkoutServiceHelper.getWorkoutServiceHelper(MainActivity.this).connect();
}
if (!preferences.isWorkoutPaused()) {
history.duration++;
history.calories = HWUtil.calculateCalorie(history, MainActivity.this);
history.cascadeSave();
}
if (history.duration % 5 == 0 && preferences.isTrace() && !preferences.isRequestSent()) {
//Sync record every five second
UserHistoryModel historyModel = history.loadAll();
if (Validator.isNotNull(historyModel)) {
UserHistory uh = convertUserHistoryModelToUserHistory(historyModel);
/*
don't set history pictures when tracing
*/
uh.setHistoryPictures(null);
uh.setId(uh.getUniqueId());
preferences.setRequestSent(true);
Request.getRequest().sendJsonRequest(Request.ADD_USER_HISTORY, MainActivity.this, MainActivity.this, RequestParameterBuilder.buildJsonObjectFromUserHistory(uh));
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
if (getSupportFragmentManager().findFragmentByTag(TabFragment.class.getName()) != null) {
TabFragment tabFragment = (TabFragment) getSupportFragmentManager().findFragmentByTag(TabFragment.class.getName());
if (tabFragment.getCurrentFragment() instanceof UpdateListener) {
((UpdateListener) tabFragment.getCurrentFragment()).update();
}
} else if (getSupportFragmentManager().findFragmentByTag(FullMapFragment.class.getName()) != null) {
FullMapFragment mapFragment = (FullMapFragment) getSupportFragmentManager().findFragmentByTag(FullMapFragment.class.getName());
mapFragment.update();
}
}
});
}
};
timer.scheduleAtFixedRate(task, 0, 1000);
}
}
每当用户开始任何锻炼时/暂停后我都会调用此方法