4

I have a Service implemented in a Module A. Now Apps B and C uses this library to bind with service using bindService(service, connection, BIND_AUTO_CREATE) but it always creates a new instance of Service. I'm using Messenger to return binder to the connection objects. If I use AIDL, how sharing the same service instance is achieved? I've read and tried almost all stackoverflow answers related to this question. But still I'm not able to achieve what I explained above.

Manifest of this Service is defined inside Module A with full process name for process attribute and exported,enabled are set to true.

 <service
        android:name="io.packagename.LocationService"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.ACCESS_FINE_LOCATION"
        android:process="io.packagename.locationService" />

LocationService-Class:

class LocationService extends Service {
        IncomingHandler handler = new IncomingHandler()
        Messenger messenger = new Messenger(handler)

        public IBinder onBind(Intent intent) {
           Log.d(TAG, "onBind")
           return messenger.binder
        }
      }

Any help is much appreciated.

Bharath Mg
  • 1,117
  • 1
  • 9
  • 18

2 Answers2

0

Quoting the docs:

As discussed in the Services document, you can create a service that is both started and bound. That is, the service can be started by calling startService(), which allows the service to run indefinitely, and also allow a client to bind to the service by calling bindService().

An example:

For example, a music player might find it useful to allow its service to run indefinitely and also provide binding. This way, an activity can start the service to play some music and the music continues to play even if the user leaves the application. Then, when the user returns to the application, the activity can bind to the service to regain control of playback.

I believe that applies to your case, so you can start the service and bind the clients to it as needed.

Mina Wissa
  • 10,923
  • 13
  • 90
  • 158
0

(I don't have enough reputation to reply above comment.)

This is across activities. I asked about sharing it across apps

Yes this applies to cross applications. I'm using this approach for 3 separated apks with different package names.

x5350
  • 16
  • 2