Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kevinmcmahon/2988931 to your computer and use it in GitHub Desktop.
Save kevinmcmahon/2988931 to your computer and use it in GitHub Desktop.
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

Nice! Very helpful!

@ashwinprasadmr
Copy link

Very Helpful and Very Simple !! Thank You

@ivanecc
Copy link

ivanecc 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

thanks great solution

@DouglasCristhian
Copy link

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

@Binary-Finery
Copy link

Binary-Finery commented Jul 31, 2018

Store a boolean value in shared preferences, simple.

@abbasnaqdi
Copy link

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

@adam-hurwitz
Copy link

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

getRunningServices is deprecated in API 30 any alternatives

@MetinSeylan
Copy link

getRunningServices is deprecated in API 30 any alternatives

ping pong with localbroadcaster

@abbasnaqdi
Copy link

@MetinSeylan Not bad idea but it's standard ?

@jhonnyfc
Copy link

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

@maitreziane
Copy link

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 }

@Tibor5
Copy link

Tibor5 commented May 17, 2023

Does anyone know if it's possible to see if an activity/service of another app is currently active/in foreground?
Working with android 10 - making a launcher for another (older) app and have to be able to see if it's already running or not (so that I don't start it twice).

@msinghal34
Copy link

@MetinSeylan Can you describe more what you meant by ping pong with localbroadcaster or give some sample code for this?

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