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
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.