Android——显示当前运行所有服务,判断服务是否运行

时间:2022-12-11 21:02:24

1.显示系统当前所有运行服务:

ActivityManager am = (ActivityManager) MainActivity.this.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : am.getRunningServices(Integer.MAX_VALUE)) {
System.out.println(service.service.getPackageName()+"-----"+service.service.getClassName());
}

2.判断某一个服务是否运行:

public static boolean checkServiceStatus(Context context) {
boolean isServiceRunning = false;
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : am
.getRunningServices(Integer.MAX_VALUE)) {
if (BackgroundService.class.getName().equals(service.service.getClassName())) {
isServiceRunning = true;
break;
}
}
return isServiceRunning;
}