How do I know if the service works?

Asked 1 years ago, Updated 1 years ago, 105 views

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

2022-09-21 17:58

1 Answers

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?


2022-09-21 17:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.