Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I extend the Binder class or use a Messenger?

In my app I've designed it to have a Service that gets data constantly (for good reason, it's from some sensors) and provides it to two clients:

  • A UI Activity to display the live data
  • Another service that logs the data

At any time, both, one or neither of these clients may be running.

I think that this service should be a Bound service, whereas the logging service is a Started service.

The Android documentation for this says I should extend the Binder class, or use a Messenger if I want to access the service from another process.

This service, the logging service and the UI Activity will all be in the same apk, so they'll presumably be in the same process - but what is going to be the best solution here? I suspect the documentation might not be taking into account the possibility that I could have two clients in the same process as the service.

Thanks

like image 435
CNorris Avatar asked Mar 05 '12 12:03

CNorris


People also ask

What is the difference between using Messenger and AIDL?

Messenger is a Handler sent to the remote process. This technique has the Binder architecture like the AIDL technique mentioned in the previous article. Messenger is much easier to implement than the AIDL technique because it creates and uses AIDL files in the background.

When to use bound services?

Services is the Android component which is used to perform long-running background tasks. There are other Android components which run in the background too, like Broadcast receiver and JobScheduler, but they are not used for long running tasks.

What does bindService do?

It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC). A bound service typically lives only while it serves another application component and does not run in the background indefinitely.

What is IBinder in Android?

IBinder: To create a bound service, you must define the interface that specifies how a client can communicate with the service. This interface between the service and a client must be an implementation of IBinder and is what your service must return from the onBind() callback method.


1 Answers

The Android documentation clearly says
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.

Using a Messenger
If you need your interface to work across different processes, you can create an interface for the service with a Messenger. In this manner, the service defines a Handler that responds to different types of Message objects. This Handler is the basis for a Messenger that can then share an IBinder with the client, allowing the client to send commands to the service using Message objects. Additionally, the client can define a Messenger of its own so the service can send messages back. This is the simplest way to perform interprocess communication (IPC), because the Messenger queues all requests into a single thread so that you don't have to design your service to be thread-safe.

So, the best option is to use a service by extending IBinder class when this service is a local service. When both services are created by using Messenger and AIDL, they are remote services.

like image 71
ρяσѕρєя K Avatar answered Sep 24 '22 14:09

ρяσѕρєя K