Use ServiceConnectionManager to manage Service connections in Android, asynchronously issue commands to a Service before it is connected, and receive Service lifecycle callbacks.
For each service that you want to manage, create an extension of the service manager:
class MyServiceConnectionManager extends ServiceConnectionManager<MyService, MyServiceBinder> {
@Override
protected Class<MyService> getServiceClass() {
return MyService.class;
}
}
Then use it in your Activity or Fragment as follows:
class MainActivity extends Activity {
private MyServiceConnectionManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Create the manager
manager = new MyServiceConnectionManager();
// Start the manager and connect to the service
manager.start();
// Run a command as soon as the service is bound
manager.runCommand(new ServiceCommand<MyServiceBinder>() {
@Override
public void run(MyServiceBinder service) {
// get something from the service
}
}
}