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;
}
Copy link

ghost commented May 8, 2016

Simple, clean, and effective! Thanks!

@whalemare
Copy link

whalemare commented Feb 23, 2017

Nice! Very helpful!

@ashwinprasadmr
Copy link

ashwinprasadmr commented May 3, 2017

Very Helpful and Very Simple !! Thank You

@IvanKalabanan
Copy link

IvanKalabanan commented May 13, 2017

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

@eakteam
Copy link

eakteam commented Oct 15, 2017

Deprecated in API 26

@gmathi
Copy link

gmathi commented Nov 17, 2017

Deprecated in API 26

@eakteam what is the better approach then?

@belrvn
Copy link

belrvn 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
Copy link

PawelSzymanski89 commented Apr 30, 2018

thanks great solution

@DouglasCristhian
Copy link

DouglasCristhian 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
Copy link

zeeshan-mehdi commented Jul 14, 2018

@Binary-Finery
Copy link

Binary-Finery commented Jul 31, 2018

Store a boolean value in shared preferences, simple.

@aaaamirabbas
Copy link

aaaamirabbas 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
Copy link

AdamSHurwitz 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 }

@theshethmilan
Copy link

theshethmilan commented Sep 11, 2020

getRunningServices is deprecated in API 30 any alternatives

@MetinSeylan
Copy link

MetinSeylan commented Oct 15, 2020

getRunningServices is deprecated in API 30 any alternatives

ping pong with localbroadcaster

@aaaamirabbas
Copy link

aaaamirabbas commented Oct 20, 2020

@MetinSeylan Not bad idea but it's standard ?

@jhonnyfc
Copy link

jhonnyfc commented Apr 27, 2021

@oky2abbas good question, have you found another way to do it?

@maitreziane
Copy link

maitreziane commented May 19, 2021

Thanks you

@gturedi
Copy link

gturedi commented Mar 7, 2022

let me write it more kotlinish way :)

@Suppress("DEPRECATION") // Deprecated for third party Services.
inline fun <reified T> Context.isServiceRunning2() =
    (getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager)
        .getRunningServices(Integer.MAX_VALUE)
        .any { it.service.className == T::class.java.name }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment