I have several Android Service
s 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 ServiceConnection
s 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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With