Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a need to have one ServiceConnection per each Service bind?

I have several Android Services that I want to bind to in my Activity, so I can monitor several actions from the user.

To be able to bind every Service, and I will have several, do I need several private ServiceConnections in my activity like the following?

/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        GPSLocalBinder gpsBinder = (GPSLocalBinder) service;
        PhotoLocalBinder photoBinder = (PhotoLocalBinder) service;
        gpsService = gpsBinder.getService();
        photoService = photoBinder.getService();
        mGpsBound = true;
        mPhotoBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mGpsBound = false;
        mPhotoBound = false;
    }
};

Or do I need a manager class between my Activity and the Services that provides better usage and understanding of the bounded Services?

like image 333
JoaoFilipeClementeMartins Avatar asked Mar 07 '14 17:03

JoaoFilipeClementeMartins


People also ask

Why do we need bind service?

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 are the components you can bind to a service?

Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

Do you know how do you create a bound service?

There are three ways of creating bound services: First is by extending the binder class. Second is by using a messenger. Third, is by using AIDL or Android Interface Definition Language.

What is the use of onBind () in android?

You can connect multiple clients to a service simultaneously. However, the system caches the IBinder service communication channel. In other words, the system calls the service's onBind() method to generate the IBinder only when the first client binds.


1 Answers

Is there a need to have one serviceConnection for each android service?

I assume you're asking if you can reuse the same serviceConnection for multiple services. There's no need to have one for each service connection, but this is probably the best approach. I see in your code this

        GPSLocalBinder gpsBinder = (GPSLocalBinder) service;
        PhotoLocalBinder photoBinder = (PhotoLocalBinder) service;
        gpsService = gpsBinder.getService();
        photoService = photoBinder.getService();

This is very confusing... this seems like a service can be cast into two different services!!

You'll realize that the onServiceConnected callback is where most of the magic happens, where you (the Activity) finally can get a pointer to your Service and start calling methods and interact with your service. If you want to reuse the same serviceConnection for different services you'd need to find out which custom subclass the IBinder object belongs to and then cast appropriately. Ufff, too much trouble. I would recommend having one serviceConnection per service.

Or do i need a manager class between my activity and the services that provides better usage and understanding of the bounded services?

For both this and your first question, you can do whatever you want. There's no approach better than the other (IMHO) and the best one is the one you understand better and makes you feel more comfortable.

like image 124
Merlevede Avatar answered Oct 20 '22 00:10

Merlevede