Issue
My question is very simple. I have a foreground service that, when activated, runs indefinitely until the user stops it. In the I MainActivity, I need to know if the service is running or not (image if the user closes the app but the foreground service is still running, when he reenters the app, I need to know if the service is already running). Is it viable to have a companion object on the Service
with a status variable so that I can access its status?
Something like this:
@AndroidEntryPoint
class SomeForegroundService: Service() {
companion object {
var status = 0
}
....
And then somewhere in my MainActivity
...
SomeForegroundService.status == 1
Is this prone to memory leaks? Whats a better solution (not counting with checking every running system service)
Solution
What you need is to bind the service
https://developer.android.com/guide/components/bound-services
If you use bindService(intent, connection, 0)
without the Context.BIND_AUTO_CREATE
flag, it will only bind to existing service, and return false if the service does not exist.
Answered By - Ricky Mo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.