Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private boolean isServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)){
if("com.example.MyNeatoIntentService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
@ghost

This comment has been minimized.

Copy link

commented May 8, 2016

Simple, clean, and effective! Thanks!

@whalemare

This comment has been minimized.

Copy link

commented Feb 23, 2017

Nice! Very helpful!

@ashwinprasadmr

This comment has been minimized.

Copy link

commented May 3, 2017

Very Helpful and Very Simple !! Thank You

@IvanKalabanan

This comment has been minimized.

Copy link

commented May 13, 2017

Cool but for performance (and memory use) better this way or static boolean flag in Service?

@eakteam

This comment has been minimized.

Copy link

commented Oct 15, 2017

Deprecated in API 26

@gmathi

This comment has been minimized.

Copy link

commented Nov 17, 2017

Deprecated in API 26

@eakteam what is the better approach then?

@Belka1000867

This comment has been minimized.

Copy link

commented Feb 12, 2018

It is still possible to use this method for your own services. From API 26 it is deprecated for third-party services only.

@PawelSzymanski89

This comment has been minimized.

Copy link

commented Apr 30, 2018

thanks great solution

@DouglasCristhian

This comment has been minimized.

Copy link

commented Jun 8, 2018

Thank you for the solution. I'm having a little bit of trouble using it for Xamarin Android, however, even if it's for my own service (It keeps telling me it's deprecated and the code fails). Anyone got a solution for it? Thank you.

@zeeshan-mehdi

This comment has been minimized.

@Binary-Finery

This comment has been minimized.

Copy link

commented Jul 31, 2018

Store a boolean value in shared preferences, simple.

@oky2abbas

This comment has been minimized.

Copy link

commented Oct 24, 2018

Optimized with Kotlin language ...

@Suppress("DEPRECATION")
fun <T> Context.isServiceRunning(service: Class<T>): Boolean {
    return (getSystemService(ACTIVITY_SERVICE) as ActivityManager)
            .getRunningServices(Integer.MAX_VALUE)
            .any { it -> it.service.className == service.name }
}
@AdamSHurwitz

This comment has been minimized.

Copy link

commented Jun 7, 2019

Thanks @oky2abbas!

Small style tweaks.

@Suppress("DEPRECATION") // Deprecated for third party Services.
fun <T> Context.isServiceRunning(service: Class<T>) =
        (getSystemService(ACTIVITY_SERVICE) as ActivityManager)
            .getRunningServices(Integer.MAX_VALUE)
            .any { it.service.className == service.name }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.