Ali
Ali

Reputation: 12664

Android - Bind to a service from an other app

I have written 2 applications. MyServiceApplication and MyActivityApplication. On boot I launch MyService which runs as a remote service. I want MyActivityApplication to communicate with it however MyActivityApplication does not have access to MyServiceApplication classes. All the examples I see online band an activity to a service in the following way:

bindService(new Intent(Binding.this, MyService.class), mConnection, Context.BIND_AUTO_CREATE);

My Application needs to communicate with MyService. The code is based off Google's MessengerService example.

The trouble as I mentioned earlier is that the service I'm trying to bind to was started by an other application whose sole purpose is to start this service.

Is there a way to do this where I can keep both applications independent? The idea is that I will have a second, third and fourth application all of whom may communicate with the same service.

Upvotes: 1

Views: 3858

Answers (2)

MikeL
MikeL

Reputation: 5611

UPDATE:
This solution and code example was valid at the time it was written at: 2015
Please look for other solution relevant for this day.

ORIGINAL ANSWER:
You can define an intent-filer for your service with a specific action and bind to this service with intent assigned to this action.

MyServiceApplication:

<service android:name=".MyService">
      <intent-filter>
        <action android:name="com.some.action.NAME" />
      </intent-filter>
</service>

MyActivityApplication:

bindService(new Intent("com.some.action.NAME"), serviceConnection, Context.BIND_AUTO_CREATE);

Upvotes: 2

Snicolas
Snicolas

Reputation: 38168

You should have a shared library between your service and application that defines the interface you use to communicate with the bound service.

Upvotes: 1

Related Questions