优化实现

TempBranch
Matthew 9 months ago
parent f8039312c5
commit 65d7ec6ff7

@ -308,7 +308,7 @@ public class MicroPhotoService extends Service {
Thread th = new Thread(runnable);
th.start();
mService.registerHeartbeatTimer();
mService.registerHeartbeatTimer(mService.mHeartbeatDuration);
try {
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
@ -431,17 +431,17 @@ public class MicroPhotoService extends Service {
if (orgHeartbeatDuration == 0) {
if (nextPhotoTime == 0) {
mHeartbeatDuration = duration;
registerHeartbeatTimer();
registerHeartbeatTimer(duration);
} else {
long ts = System.currentTimeMillis();
nextPhotoTime *= 1000;
if (nextPhotoTime > ts) {
mHeartbeatDuration = (int) ((nextPhotoTime - ts) % duration) + 999;
registerHeartbeatTimer(false);
mHeartbeatDuration = duration;
registerHeartbeatTimer((int) ((nextPhotoTime - ts) % duration) + 999);
} else {
mHeartbeatDuration = duration;
registerHeartbeatTimer();
registerHeartbeatTimer(duration);
}
}
} else {
@ -449,27 +449,18 @@ public class MicroPhotoService extends Service {
}
}
private void registerHeartbeatTimer() {
registerHeartbeatTimer(true);
}
private void registerHeartbeatTimer(boolean normalDuration) {
private void registerHeartbeatTimer(long timeout) {
// 创建延迟意图
long triggerTime = System.currentTimeMillis() + mHeartbeatDuration;
long triggerTime = System.currentTimeMillis() + timeout;
Intent alarmIntent = new Intent();
alarmIntent.setAction(ACTION_HEARTBEAT);
if (normalDuration) {
alarmIntent.putExtra("HeartbeatDuration", mHeartbeatDuration);
}
alarmIntent.putExtra("HeartbeatDuration", mHeartbeatDuration);
alarmIntent.putExtra("HeartbeatTime", triggerTime);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
// mNextHeartbeatTime = System.currentTimeMillis() + mHeartbeatDuration;
// alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + timeout, pendingIntent);
}
private static void registerPhotoTimer(Context context, long scheduleTime, long takingTime, long timeout, List<Long> schedules) {

Loading…
Cancel
Save