Can an Android service provide two interfaces to communicate with?
Asked Answered
G

2

9

I have a service that communicates through AIDL with other services. I want that service to be bound by activities in my application. Can the service define two binders\interfaces? I've tried yo use a messenger for communicating with the activities, overriding "onBind" method so that it returns a different binder according to the intent it gets (one for the other services and one for the activities).

But when the activities (that use the same binder) unbind from the service, I have an error "myService has leaked ServiceConnection ... that was originally bound here", which I believe is about the binder the service use to communicate with the other services.
If a service cant use two interfaces, how can I implement the communication between the activities and that service?

thank you, -Liron

Gora answered 23/12, 2012 at 16:13 Comment(0)
E
2

If by

"overriding "onBind" method so that it returns a different binder according to the intent it gets "

You mean, that you set an extra to your Intent, indicating what to do it won't work. According to the Docs in onBind(Intent):

Intent: The Intent that was used to bind to this service, as given to Context.bindService. Note that any extras that were included with the Intent at that point will not be seen here.

Try to give your intent a custom action and check if that works

Esparza answered 12/4, 2019 at 9:52 Comment(0)
P
0

AIDL and Messenger are used for IPC with other applications/processes. From the Android API Guide:

Note: Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service. If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder or, if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger. Regardless, be sure that you understand Bound Services before implementing an AIDL.

If your activities are in the same process as the service, you just need to extend Binder.

Extending the Binder class

If your service is private to your own application and runs in the same process as the client (which is common), you should create your interface by extending the Binder class and returning an instance of it from onBind(). The client receives the Binder and can use it to directly access public methods available in either the Binder implementation or even the Service. This is the preferred technique when your service is merely a background worker for your own application. The only reason you would not create your interface this way is because your service is used by other applications or across separate processes.

This graphic regarding the bound service lifecycle may help with how you are binding/unbinding (http://developer.android.com/guide/components/bound-services.html#Lifecycle):

enter image description here

Pollinosis answered 30/1, 2013 at 1:27 Comment(1)
Answer is off-topic because OP is well aware when to use what. His question was if he can return different Binder in onBind depending on the Intent send. You answered when to use which binder.Esparza

© 2022 - 2024 — McMap. All rights reserved.