Android – binding to running Service (after finish() ) / callback Handler


Again a question about LocalServices. How do I (re-)bind to an existing Service, after onDestroy()?

The Problem:
I'm binding to a Service and Starting the service from an Activity. I'm Posting runnable Objects to the Binder, for a callback (updating a progressbar) on the UI. When I close this Activity, the OS could end the lifecycle and Destroy the Activity, calling onDestroy(), right? I simulate this, calling finish() in onPause() method. So once I restart the Activity, how to I bind to the SAME Service again? I thought that Services are Singelton, but when I'm trying to re-bind, I get another binder reference. So; still has the reference to the old binder/callback/progressHandler, not to my new one.
Even the Constructor of the Service is called again!

Is there any solution to have a progressbar, getting updated by callback objects from the service (working). Closing/onDestroy() the Activity. Come back, and continue the progressbar?

My code is quite large, but recreated the Szenario:

    public class MyService extends Service {
        private final LocalBinder binder = new LocalBinder();

        public class LocalBinder extends Binder implements TestRunServiceBinder {
            private Handler callbackHandler;
            private ServiceStartActivity.RunOnServiceProgress onProgress;

            public void setActivityCallbackHandler(Handler messageHandler) {
                callbackHandler = messageHandler;

            public void setServiceProgressHandler(RunOnServiceProgress runnable) {
                onProgress = runnable;

                public void doSomething(){


       private void _doSomething(){
           //do this a couple of times (could take up to 10min)



public class ServiceStartActivity{
 private final Handler messageHandler = new Handler();
     private ServiceConnection mTestServiceConnection = new ServiceConnection() {

        public void onServiceDisconnected(ComponentName name) {
            testRunBinder = null;

        public void onServiceConnected(ComponentName name, IBinder service) {
            testRunBinder = (TestRunServiceBinder) service;
            testRunBinder.setServiceProgressHandler(new RunOnServiceProgress());

     protected void onStart() {

    // bind to the Service
    final Intent serviceIntent = new Intent(ServiceStartActivity.this,
            mTestServiceConnection, Context.BIND_AUTO_CREATE);

     protected void onStop() {
       public class RunOnServiceProgress implements Runnable {
            public void run() {
                //do something on the UI!

Best Solution

I got it now. The solution is to explicit call startService(serviceIntent); before you bind to the Service using getApplicationContext().bindService(serviceIntent,mTestServiceConnection, Context.BIND_AUTO_CREATE);

Reason: When you start a Service with bindService(), it becomes a Bound Service an

runs only as long as another application component is bound to it.

If you start a Service with startService() it can

can run in the background indefinitely,

So if you have e.g. a progessbar on the UI, and you want it to continue updating it, you should start your Service, and bind and undbind it in onResume() / onPause(). But be carfull: Since you started the Service manually, You should also stop it manually. The simplest way to do this is call stopSelf() once the Service did it's work.

This soultion covers a proper binding from an Activity with e.g. an progresss bar to the same Service even after the activity is destroyed or after an orientation change.

Related Question