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

前端 未结 1 1773
无人及你
无人及你 2021-02-10 04:27

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

相关标签:
1条回答
  • 2021-02-10 05:07

    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.

    0 讨论(0)
提交回复
热议问题