I read some similar questions (for example at this link), but the problem I'm asking is a bit different. In fact, in my case the service is started manually by the startService
method, then as a consequence it can not be started using the bindService
method.
- Suppose we have a package that contains the
MainService
service andMainServiceActivity
activity. In the file "AndroidManifest.xml" this activity is declared with actionMAIN
and categoryLAUNCHER
. This activity is used to configure the service via theSharedPreferences
and start the service by invokingstartService
method. In other words, typically the user launches theMainServiceActivity
and configures/starts theMainService
. - Now consider another activity (Let's call it
SecondActivity
) that is part of another package. Depending on the configuration, the service starts this activity using thestartActivity
method, so this other activity is running on a separate process than theMainService
. As soon as the activity is running, it should inform the service. - At this point, a communication request/reply begins between the
MainService
and theSecondActivity
: the service sends a request and the activity sends a reply.
The communication via messaging might fit, but the MainService
is started through startService
method, so the bindService
method can not be invoked by activities that want to bind to the service.
Then I had an idea that makes use of an additional service (Let's call it UtilityService
), which is part of the same package of MainService
: the UtilityService
could be started using the bindService
method. As a consequence:
- as soon as the
MainService
is running, it might perform the bind to theUtilityService
; - when the
MainService
launches an external activity (for example the aboveSecondActivity
), this activity bind to theUtilityService
.
In this way, both the MainService
and the SecondActivity
are connected to the UtilityService
, where the latter acts as an intermediary for communication.
Are there alternatives to this idea?