From 87dbbb00d7a474b8199ebeaab0e981b6e7913a7f Mon Sep 17 00:00:00 2001 From: Matthew Date: Fri, 18 Apr 2025 17:43:50 +0800 Subject: [PATCH] =?UTF-8?q?Mpapp=E5=AF=B9=E8=BF=90=E7=BB=B4app=E4=BF=9D?= =?UTF-8?q?=E6=B4=BB=E6=9C=BA=E5=88=B6=E7=9A=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 避免运维app启动多个activity --- .../com/xypower/mpapp/MicroPhotoService.java | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index e80d1e3d..0faea91d 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -400,10 +400,38 @@ public class MicroPhotoService extends Service { public AlarmReceiver() { mService = null; } + public AlarmReceiver(MicroPhotoService service) { mService = service; } + + private void keepMpMasterAlive(final Context context, long ts) { + try { + File mpmstLogFile = new File(MicroPhotoContext.buildMasterAppDir(context) + "logs/mlog.txt"); + long mlogTime = mpmstLogFile.exists() ? mpmstLogFile.lastModified() : 0; + if ((ts - mlogTime) > 1800000 /*86400000*/) { + // More than 1 day, try restart it + Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(MicroPhotoContext.PACKAGE_NAME_MPMASTER); + if (launchIntent != null) { + launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); + context.startActivity(launchIntent); + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + @Override public void onReceive(Context context, Intent intent) { + try { + processNotification(context, intent); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + private void processNotification(final Context context, final Intent intent) { String action = intent.getAction(); if (TextUtils.equals(ACTION_HEARTBEAT, action)) { Log.i(TAG, "HB Timer Fired ACTION=" + action); @@ -426,31 +454,12 @@ public class MicroPhotoService extends Service { mService.registerHeartbeatTimer(timeout); try { - mService.detectGpsStatus(); - - ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); - if (connectivityManager != null) { - if (!connectivityManager.isDefaultNetworkActive()) { - infoLog("DefaultNetwork is NOT Active"); - } - } } catch (Exception ex) { ex.printStackTrace(); } - try { - Date dt = new Date(); - if ((dt.getHours() == 1 || dt.getHours() == 2) && dt.getMinutes() > 35 && dt.getMinutes() < 55) { - Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(MicroPhotoContext.PACKAGE_NAME_MPMASTER); - // launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); - if (launchIntent != null) { - context.startActivity(launchIntent); - } - } - } catch (Exception ex) { - ex.printStackTrace(); - } + keepMpMasterAlive(context, ts); } else if (TextUtils.equals(ACTION_TAKE_PHOTO, action)) { long ts = intent.getLongExtra(EXTRA_PARAM_TIME, 0); int cnt = intent.getIntExtra(EXTRA_PARAM_SCHEDULES, 0);