How do I check if the background service is running when I want to turn the status of the service on and off?
android android-service
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
isMyServiceRunning(MyService.class)
Do you want to try this?
© 2024 OneMinuteCode. All rights reserved.