Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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;
}
@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).

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