From fa08064340abea069a9234557046315735c15f51 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 19 Dec 2024 10:23:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=BF=83=E8=B7=B3=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E5=92=8C=E7=9B=B8=E5=85=B3=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xypower/mpapp/MicroPhotoService.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index a401fea5..c2baf2ee 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -101,6 +101,10 @@ public class MicroPhotoService extends Service { public final static int MSG_WHAT_MAX = 1000; + public final static int BROADCAST_REQUEST_CODE_HEARTBEAT = 1; + public final static int BROADCAST_REQUEST_CODE_TAKING_PHOTO = 2; + public final static int BROADCAST_REQUEST_CODE_GPS = 2; + public static final int NOTIFICATION_ID_FOREGROUND_SERVICE = 8466503; public static final String ACTION_MSG_BROADCAST = "ACT_MSG_BROADCAST"; @@ -422,11 +426,12 @@ public class MicroPhotoService extends Service { mService.sendHeartbeat(mService.mNativeHandle, mService.getSignalLevel()); } }; - Thread th = new Thread(runnable); th.start(); - - mService.registerHeartbeatTimer(mService.mHeartbeatDuration); + + long expectedHbTime = intent.getLongExtra("HeartbeatTime", 0); + long timeout = (expectedHbTime != 0) ? (expectedHbTime + mService.mHeartbeatDuration - System.currentTimeMillis()) : mService.mHeartbeatDuration; + mService.registerHeartbeatTimer(timeout); try { @@ -653,7 +658,7 @@ public class MicroPhotoService extends Service { alarmIntent.setAction(ACTION_HEARTBEAT); alarmIntent.putExtra("HeartbeatDuration", mHeartbeatDuration); alarmIntent.putExtra("HeartbeatTime", triggerTime); - PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent pendingIntent = PendingIntent.getBroadcast(this, BROADCAST_REQUEST_CODE_HEARTBEAT, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); Date date = new Date(triggerTime); String dateStr = (String) DateFormat.format("kk:mm:ss", date); @@ -696,7 +701,7 @@ public class MicroPhotoService extends Service { LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context); localBroadcastManager.sendBroadcast(intent); } else { - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, BROADCAST_REQUEST_CODE_TAKING_PHOTO, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE); try { @@ -705,12 +710,12 @@ public class MicroPhotoService extends Service { ex.printStackTrace(); } + alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeout, pendingIntent); + long currentTimeMillis = System.currentTimeMillis(); Date date = new Date(currentTimeMillis + timeout); String dateStr = (String) DateFormat.format("MM-dd kk:mm:ss", date); infoLog( "Timer Reg: " + dateStr + " TS=" + currentTimeMillis + " Timeout=" + timeout + " " + channelStr.toString()); - - alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeout, pendingIntent); } } @@ -1405,7 +1410,7 @@ public class MicroPhotoService extends Service { Intent intent = new Intent(); intent.setAction(ACTION_GPS_TIMEOUT); - mPreviousGpsTimer = PendingIntent.getBroadcast(this, 0, intent, 0); + mPreviousGpsTimer = PendingIntent.getBroadcast(this, BROADCAST_REQUEST_CODE_GPS, intent, 0); alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + mGpsTimeout, mPreviousGpsTimer); } catch (Exception ex) {