Android Binding Service - Should I manually connect to onServiceDisconnected or try to connect again?

If I disconnected from the linked service due to some unforeseen circumstances, after I called, should I manually connect to onServiceDisconnected or try to reconnect?

public class MyServiceConnection extends Activity implements ServiceConnection { MyBinder binder; @Override protected void onStart() { super.onStart(); connect(); } private void connect() { bindService(new Intent(this, MyService.class), this, Service.BIND_AUTO_CREATE); } @Override public void onServiceConnected(ComponentName name, IBinder service) { binder = (MyBinder) service; } @Override public void onServiceDisconnected(ComponentName name) { binder = null; //should i reconnect here ? connect(); } } 
+7
source share
2 answers

According to the ServiceConnection API :

public abstract void onServiceDisconnected (computer_name)

Called when the connection to the Service is lost. This usually happens when the process on which the service is running crashed or was killed. This will not delete the ServiceConnection itself - this service binding will remain active, and you will receive an onServiceConnected (ComponentName, IBinder) call when the service is started.

Back to your question:

 @Override public void onServiceDisconnected(ComponentName name) { binder = null; //should i reconnect here ? connect(); } 

It all depends on what process serves the real service.

Local service:

A service runs in the same process as other components (i.e. related activities) from the same application, when this one process with the application area crashed or was killed, it is very likely that all components of this process (include activities associated with this service) are also destroyed. In this case, the call to connect () inside onServiceDisconnected () has no effect, since when the application process is restored, everything jumps from the very beginning, and the activity is recreated again, and the service is associated with the active call to onStart ().

Remote Service:

The service starts in a separate process, when this process crashed or was killed, only the actual service is destroyed, the activity lives in another process that is attached to the service, it remains, therefore, it is normal to call connect () in its onServiceDisconnected () callback to recreate / re-link the service.

Browse here to find out how to configure the service to start on a separate process in AndroidManifest.xml.

+8
source

Found this old question while doing research for a blog post. The accepted answer is good enough, but it doesn't show the whole picture when it comes to related IPC (remote) services.

The status diagram of the connection to the IPC related service is as follows:

enter image description here

And the answer to the OP question: it depends.

First of all, when a service crashes or is killed by the OS, it remains connected . Thus, the question arises whether the system recreates the service or not ...

  • In the event that the associated IPC service is killed by the OS, it will be recreated and you will receive an onServiceConnected() call, so there is no need to "reconnect".
  • In case of failure of the IPC service, the system will try to recreate it once - if it works a second time, the system will not recreate it again (checked by JellyBean, Lollipop and Marshmallow).

Theoretically, you could have unbindService() and then bindService() with every call to onServiceDisconnected() - this should work (I think).

However, writing a truly reliable client for the IPC related service is a bit more work, and a general idea (along with a tutorial application) can be found here .

+3
source

All Articles