保活时判断service是否存在

lowmem
Matthew 2 months ago
parent 21c789155c
commit 3fab6d4542

@ -472,7 +472,7 @@ public class MicroPhotoService extends Service {
} }
try { try {
boolean mpmstAlive = MicroPhotoContext.isAppAlive(context, MicroPhotoContext.PACKAGE_NAME_MPMASTER); boolean mpmstAlive = MicroPhotoContext.isAppAlive(context, MicroPhotoContext.PACKAGE_NAME_MPMASTER, MicroPhotoContext.SERVICE_NAME_MPMASTER);
if (!mpmstAlive) { if (!mpmstAlive) {
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(MicroPhotoContext.PACKAGE_NAME_MPMASTER); Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(MicroPhotoContext.PACKAGE_NAME_MPMASTER);
// launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); // launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

@ -30,6 +30,10 @@ public class MicroPhotoContext {
public static final String PACKAGE_NAME_MPAPP = "com.xypower.mpapp"; public static final String PACKAGE_NAME_MPAPP = "com.xypower.mpapp";
public static final String PACKAGE_NAME_MPMASTER = "com.xypower.mpmaster"; public static final String PACKAGE_NAME_MPMASTER = "com.xypower.mpmaster";
public static final String SERVICE_NAME_MPSERVICE = PACKAGE_NAME_MPAPP + ".MicroPhotoService";
public static final String SERVICE_NAME_MPMASTER = PACKAGE_NAME_MPMASTER + ".MpMasterService";
public static final String ACTION_HEARTBEAT_MP = "com.xypower.mpapp.ACT_HB"; public static final String ACTION_HEARTBEAT_MP = "com.xypower.mpapp.ACT_HB";
public static final String ACTION_RESTART_MP = "com.xypower.mpapp.ACT_RESTART"; public static final String ACTION_RESTART_MP = "com.xypower.mpapp.ACT_RESTART";
public static final String ACTION_UPDATE_CONFIGS_MP = "com.xypower.mpapp.ACT_UPD_CFG"; public static final String ACTION_UPDATE_CONFIGS_MP = "com.xypower.mpapp.ACT_UPD_CFG";
@ -141,13 +145,13 @@ public class MicroPhotoContext {
return str; return str;
} }
public static boolean isAppAlive(Context context, String packageName) { public static boolean isAppAlive(Context context, String packageName, String serviceClassName) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> services = am.getRunningServices(Integer.MAX_VALUE); List<ActivityManager.RunningServiceInfo> services = am.getRunningServices(Integer.MAX_VALUE);
boolean isRunning = false; boolean isRunning = false;
for (ActivityManager.RunningServiceInfo rsi : services) { for (ActivityManager.RunningServiceInfo rsi : services) {
if (packageName.equalsIgnoreCase(rsi.service.getPackageName())) { if (packageName.equalsIgnoreCase(rsi.service.getPackageName()) && TextUtils.equals(serviceClassName, rsi.service.getClassName())) {
isRunning = true; isRunning = true;
break; break;
} }

@ -425,7 +425,7 @@ public class MpMasterService extends Service {
} }
boolean isMpAppRunning = false; boolean isMpAppRunning = false;
for (int idx = 0; idx < detectionCnt; idx++) { for (int idx = 0; idx < detectionCnt; idx++) {
isMpAppRunning = MicroPhotoContext.isAppAlive(context, MicroPhotoContext.PACKAGE_NAME_MPAPP); isMpAppRunning = MicroPhotoContext.isAppAlive(context, MicroPhotoContext.PACKAGE_NAME_MPAPP, MicroPhotoContext.SERVICE_NAME_MPSERVICE);
if (isMpAppRunning) { if (isMpAppRunning) {
break; break;
} }

@ -95,7 +95,7 @@ public class UpdateReceiver extends BroadcastReceiver {
private void startMpApp(final Context context, String reason) { private void startMpApp(final Context context, String reason) {
try { try {
if (MicroPhotoContext.isAppAlive(context, MicroPhotoContext.PACKAGE_NAME_MPAPP)) { if (MicroPhotoContext.isAppAlive(context, MicroPhotoContext.PACKAGE_NAME_MPAPP, MicroPhotoContext.SERVICE_NAME_MPSERVICE)) {
return; return;
} }

Loading…
Cancel
Save