-
-
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; | |
} |
Deprecated in API 26
@eakteam what is the better approach then?
It is still possible to use this method for your own services. From API 26 it is deprecated for third-party services only.
thanks great solution
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.
This did not worked for me but this did https://stackoverflow.com/questions/18094982/detect-if-my-accessibility-service-is-enabled?rq=1
Store a boolean value in shared preferences, simple.
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 }
}
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 }
getRunningServices is deprecated in API 30 any alternatives
getRunningServices is deprecated in API 30 any alternatives
ping pong with localbroadcaster
@MetinSeylan Not bad idea but it's standard ?
@oky2abbas good question, have you found another way to do it?
Thanks you
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 }
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).
Deprecated in API 26