Reputation: 3821
I want to use a started (foreground) service to manage a network connection that should persist when the user leaves the application for a short time, and that the user should be aware of (so he can return to the app and maybe disconnect). This service will only ever be used locally by activities in the same process.
Maybe it's just because I am new to Android, but I find it unnecessarily difficult to bind to this service in every activity that uses it - in particular, the asynchronous nature of binding, which only really seems to be necessary for accessing services in a different process. Is there any indication against just accessing the started service through a static variable instead?
Upvotes: 2
Views: 286
Reputation: 10672
Maybe I'm understanding your question wrong, but there is no need to bind to the started Service
from every Activity
. Instead, you could simply start the Service
from wherever you need to interact with it. This calls the onStartCommand()
if the Service
is already started. You could include an extra with the Intent
that starts the Service
to distinguish between the first start and subsequent ones.
Of course - this addresses the use case where you do not want to have a client-server mode of interaction between your activities and the Service
- that scenario requires binding and if you really need binding, then you need to bind from every component that needs to be served by the Service
.
Upvotes: 1