Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android bound service - should I manually reconnect in onServiceDisconnected or it tries reconnect automatically?

If i get disconnected from bound service due to some unexpected circumstances, after i called, should I manually reconnect in onServiceDisconnected or it tries to reconnect automatically?

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();
    }
}
like image 623
Tomasz Gawel Avatar asked May 22 '12 09:05

Tomasz Gawel


People also ask

How does bound service work in Android?

It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC). A bound service typically lives only while it serves another application component and does not run in the background indefinitely.

How do you start a bound service?

public class APIService extends Service implements APICall{ private final IBinder binder = new APIBinder(); @Override public void onCreate() { super. onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId){ super. onStartCommand(intent, flags, startId); return Service.

What is bounded and unbounded services in Android?

Bounded services are bounded to an activity which binds it and will work only till bounded activity is alive. while a unbounded service will work till the completion even after activity is destroyed.


1 Answers

According to the ServiceConnection API:

public abstract void onServiceDisconnected (ComponentName name)

Called when a connection to the Service has been lost. This typically happens when the process hosting the service has crashed or been killed. This does not remove the ServiceConnection itself -- this binding to the service will remain active, and you will receive a call to onServiceConnected(ComponentName, IBinder) when the Service is next running.

Back to your question:

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

It is all depend on which process the actual service lives.

Local Service:

Service is running in the same process as other components (i.e. activity that bound to it) from the same application, when this single application-scoped process has crashed or been killed, it is very likely that all components in this process (include the activity that bound to this service) are also destroyed. In this case, calling connect() inside onServiceDisconnected() doesn't make any effect, as when application process is recovered, everything is rolling over from very beginning and the activity is recreated again and service is bound in activity's onStart() callback.

Remote Service:

Service is running in separate process, when this process has crashed or been killed, only the actual service is destroyed, the activity lives in another process that bound to the service is remained, so it is probably OK to call connect() in its onServiceDisconnected() callback in order to re-create/re-bind the service.

Check out here to see how to configure service running on separate process in AndroidManifest.xml.

like image 57
yorkw Avatar answered Nov 03 '22 01:11

yorkw