Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When exactly onServiceConnected for bounded service will be called?

I am trying to bind service from another service like this:

public class ServiceA extends Service {
    private ServiceB mDataService;
    private boolean mIsBound;

    @Override
    public void onCreate(){
        super.onCreate();
        doBindService();
        /* ... */
    }

    @Override
    public void onStart(final Intent intent, final int startId){
        /*...*/
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {            
            mDataService = ((ServiceB.LocalBinder)service).getService();
        }
        public void onServiceDisconnected(ComponentName className) {             
            mDataService = null;
        }
    };

    void doBindService() {          
        bindService(new Intent(ServiceA.this, ServiceB.class), mConnection, Context.BIND_AUTO_CREATE);          
        mIsBound = true;
    }

    void doUnbindService() {
        if (mIsBound) {                     
            unbindService(mConnection);
            mIsBound = false;
        }
    }       
}

This is a simple snippet that I took from goolge's samples :) The code works just fine and mDataService holds a reference to ServiceB instance, but there is one thing I could not understand: the onServiceConnected callback is called after the call to onStart. As I saw on android's docs, the callback is running on the main thread - but can I count on it that it will ALWAYS happen in this order in my case? onCreate -> onStart -> onServiceConnected ?

like image 393
ET. Avatar asked May 29 '12 15:05

ET.


People also ask

When onServiceConnected is called in Android?

onServiceConnected. Called when a connection to the Service has been established, with the IBinder of the communication channel to the Service. Note: If the system has started to bind your client app to a service, it's possible that your app will never receive this callback.

How to start a bound service?

Binding to a started service That is, you can start a service by calling startService() , which allows the service to run indefinitely, and you can also allow a client to bind to the service by calling bindService() .


2 Answers

If the official dev guide is (still) not clear, the Context.bindService() is indeed an asynchronous call. This also explains why ServiceConnection.onServiceConnected() is implemented as a callback.

Check out the dev guide:

A client binds to a service by calling bindService(). When it does, it must provide an implementation of ServiceConnection, which monitors the connection with the service.

The return value of bindService() indicates whether the requested service exists and whether the client is permitted access to it.

When the Android system creates the connection between the client and service, it calls onServiceConnected() on the ServiceConnection. The onServiceConnected() method includes an IBinder argument, which the client then uses to communicate with the bound service.

ServiceConnection.onServiceConnected() is called on UI thread at some point in the future (not immediately after calling Context.bindService()), once connection to service is properly established.

like image 150
yorkw Avatar answered Sep 20 '22 13:09

yorkw


I wouldn't rely on it. It depends if ServiceA and ServiceB are running in the same or in different processes. It probably also depends if ServiceB has already been started. You should write your code so that you don't depend on this sequence of events.

like image 42
David Wasser Avatar answered Sep 20 '22 13:09

David Wasser