增加一种保活机制

心跳时设置一个系统服务延迟启动运维app的闹钟,并在下一次心跳时重置闹钟时间。
lowmem
Matthew 4 weeks ago
parent a22f41d537
commit 4eb14e755c

@ -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);

Loading…
Cancel
Save