public static List<Boolean> isTwoServiceRunning(Context context, String ServiceName1, String ServiceName2) {
Boolean serviceNull1 = ("").equals(ServiceName1) || ServiceName1 == null;
Boolean serviceNull2 = ("").equals(ServiceName2) || ServiceName2 == null;
List<Boolean> servicesBoolean = new ArrayList<>();
if (serviceNull1 && serviceNull2) {
return null;
}
Log.d(TAG, "isServiceRunning: " + serviceNull1 + ", " + serviceNull2);
Boolean serviceOne =false;
Boolean serviceTwo= false;
ActivityManager myManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
ArrayList<ActivityManager.RunningServiceInfo> runningService = (ArrayList<ActivityManager.RunningServiceInfo>) myManager
.getRunningServices(100);
for (int i = 0; i < runningService.size(); i++) {
if (serviceOne && serviceTwo) {
break;
}
if (!serviceNull1 && !serviceOne && runningService.get(i).service.getClassName().equals(ServiceName1)) {
Log.d(TAG, "isService1Running: ");
serviceOne = true;
}
if (!serviceNull2 && !serviceTwo && runningService.get(i).service.getClassName().equals(ServiceName2)) {
Log.d(TAG, "isService2Running: ");
serviceTwo = true;
}
//Log.d(TAG, "isServiceRunning: not running" + runningService.get(i).service.getClassName() + i);
}
Log.d(TAG, "isServiceRunning: " + serviceOne + ", " + serviceTwo);
servicesBoolean.add(serviceOne);
servicesBoolean.add(serviceTwo);
return servicesBoolean;
}
List<Boolean> serviceBoolean = CommonUtils.isTwoServiceRunning(this,"Service1",
"Service2");
if(serviceBoolean == null) {
return;
}
startSingleService(serviceBoolean.get(0), ServiceIntent1);
startSingleService(serviceBoolean.get(1), ServiceIntent2);
private void startSingleService(Boolean isStart, Intent intent) {
if (isStart) {
return;
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
startForegroundService(intent);
} else {
startService(intent);
}
}