From 4eb14e755c5df336ac65a222ceade9afa75c3136 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sun, 18 May 2025 21:00:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E7=A7=8D=E4=BF=9D?= =?UTF-8?q?=E6=B4=BB=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 心跳时设置一个系统服务延迟启动运维app的闹钟,并在下一次心跳时重置闹钟时间。 --- .../com/xypower/mpmaster/MpMasterService.java | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java index df085d41..3407a6b7 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java @@ -145,9 +145,15 @@ public class MpMasterService extends Service { private String mIccid1 = null; private String mIccid2 = null; + public final static int BROADCAST_REQUEST_CODE_NOTIFICATION = 11; + public final static int BROADCAST_REQUEST_CODE_NOTIFICATION_STOP = 12; + public final static int BROADCAST_REQUEST_CODE_HEARTBEAT = 15; + public final static int BROADCAST_REQUEST_CODE_REAL_HEARTBEAT = 16; + public final static int BROADCAST_REQUEST_CODE_SYS_KEEPALIVE = 17; + public final static int BROADCAST_REQUEST_CODE_RELAUNCH = 18; + //用于创建单例线程保证该线程在项目中唯一 private static final AtomicBoolean created = new AtomicBoolean(false); - AtomicInteger reqCode = new AtomicInteger(0); public MpMasterService() { } @@ -157,8 +163,7 @@ public class MpMasterService extends Service { final Context context = getApplicationContext(); try { Intent intent = new Intent(this, MainActivity.class); - int uniqueReqCode = reqCode.getAndIncrement(); - PendingIntent pi = PendingIntent.getActivity(this, uniqueReqCode, intent, 0); + PendingIntent pi = PendingIntent.getActivity(this, BROADCAST_REQUEST_CODE_RELAUNCH, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pi); logger.info("Restart MpApp after 5s as for LowMemory"); @@ -857,8 +862,8 @@ public class MpMasterService extends Service { if (keepAlive) { alarmIntent.putExtra("keepAlive", keepAlive); } -// PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueReqCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); - PendingIntent pendingIntent = PendingIntent.getBroadcast(this, keepAlive ? 0 : 1, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); + int reqCode = keepAlive ? BROADCAST_REQUEST_CODE_HEARTBEAT : BROADCAST_REQUEST_CODE_REAL_HEARTBEAT; + PendingIntent pendingIntent = PendingIntent.getBroadcast(this, reqCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); Date dt = new Date(triggerTime); @@ -870,6 +875,16 @@ public class MpMasterService extends Service { mPreviousHB = pendingIntent; mPreviousHeartbeatTime = triggerTime; alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent); + + Intent relaunchIntent = new Intent(); + relaunchIntent.putExtra("cmd", "forceLaunch"); + relaunchIntent.putExtra("pkname", MicroPhotoContext.PACKAGE_NAME_MPMASTER); + relaunchIntent.setAction("com.xy.xsetting.action"); + relaunchIntent.setPackage("com.android.systemui"); + // getApplicationContext().sendBroadcast(restartIntent); + + PendingIntent sysKAPendingIntent = PendingIntent.getBroadcast(this, BROADCAST_REQUEST_CODE_SYS_KEEPALIVE, relaunchIntent, PendingIntent.FLAG_UPDATE_CURRENT); + alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + (mHeartbeatDuration + 2) * 1000, sysKAPendingIntent); } @Override @@ -967,14 +982,12 @@ public class MpMasterService extends Service { notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); }*/ - int uniqueReqCode = reqCode.getAndIncrement(); - PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueReqCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent pendingIntent = PendingIntent.getActivity(this, BROADCAST_REQUEST_CODE_NOTIFICATION, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); // make a stop intent Intent stopIntent = new Intent(this, MpMasterService.class); stopIntent.setAction(ACTION_STOP); - uniqueReqCode = reqCode.getAndIncrement(); - PendingIntent pendingStopIntent = PendingIntent.getService(this, uniqueReqCode, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent pendingStopIntent = PendingIntent.getService(this, BROADCAST_REQUEST_CODE_NOTIFICATION_STOP, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification); remoteViews.setOnClickPendingIntent(R.id.btn_stop, pendingStopIntent);