优化实现

TempBranch
Matthew 9 months ago
parent f8039312c5
commit 65d7ec6ff7

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

Loading…
Cancel
Save