How to determine if an Android Service is running in the foreground?

后端 未结 6 934
眼角桃花
眼角桃花 2020-11-29 04:31

I have a service which I believe to have running in the foreground, How do I check if my implementation is working?

6条回答
  •  时光说笑
    2020-11-29 05:27

    Little touch on Adam's answer:

    @Suppress("DEPRECATION") // Deprecated for third party Services.
    fun  Context.isServiceForegrounded(service: Class) =
        (getSystemService(ACTIVITY_SERVICE) as? ActivityManager)
            ?.getRunningServices(Integer.MAX_VALUE)
            ?.find { it.service.className == service.name }
            ?.foreground == true
    

提交回复
热议问题