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