I have an Android application that is binding to a persistent service (once started with startService()
).
The service is an integral part of the application and thus is used in almost every Activity. Hence I want to bind to the service just once (instead of binding/unbinding in every Activity) and keep the binding during the lifetime of my application.
I've extended from Application and bind to the service in Application#onCreate(). However I now have the problem that I don't know when my application exists since Application#onTerminate() is never called, see JavaDoc:
This method is for use in emulated process environments. It will never be called on a production Android device, where processes are removed by simply killing them; no user code (including this callback) is executed when doing so.
So how do I cleanly unbind from a service bound in Application?
Application components (clients) can bind to a service by calling bindService() . The Android system then calls the service's onBind() method, which returns an IBinder for interacting with the service. The binding is asynchronous, and bindService() returns immediately without returning the IBinder to the client.
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.
A service is termed as bounded when an application component binds itself with a service by calling bindService() method. To stop the execution of this service, all the components must unbind themselves from the service by using unbindService() method.
Bound service: To create a bound service, implement the onBind() callback method to return an IBinder that defines the interface for communication with the service. Other application components can then call bindService() to retrieve the interface and begin calling methods on the service.
I solved this problem by counting the references to the service binding in the Application
. Every Activity
has to call acquireBinding()
in their onCreate()
methods and call releaseBinding()
in onDestroy()
. If the reference counter reaches zero the binding is released.
Here's an example:
class MyApp extends Application {
private final AtomicInteger refCount = new AtomicInteger();
private Binding binding;
@Override
public void onCreate() {
// create service binding here
}
public Binding acquireBinding() {
refCount.incrementAndGet();
return binding;
}
public void releaseBinding() {
if (refCount.get() == 0 || refCount.decrementAndGet() == 0) {
// release binding
}
}
}
// Base Activity for all other Activities
abstract class MyBaseActivity extend Activity {
protected MyApp app;
protected Binding binding;
@Override
public void onCreate(Bundle savedBundleState) {
super.onCreate(savedBundleState);
this.app = (MyApp) getApplication();
this.binding = this.app.acquireBinding();
}
@Override
public void onDestroy() {
super.onDestroy();
this.app.releaseBinding();
}
}
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