dzhioev
dzhioev

Reputation: 96

How to delay onServiceConnected call

I'm implementing Service that establishes TCP connection with server and then allows clients to pass messages through this connection. Clients connect to service with bindService call. As a result onServiceConnected called in clients ServiceConnection object. The problem is that onServiceConnected called right after return from bindService, but my Service was not established connection with server at this moment. Can I somehow delay onServiceConnected call while connection was not established? If it is not possible please suggest some good pattern for my case. Thank you.

Upvotes: 1

Views: 775

Answers (1)

Ganesh Bhambarkar
Ganesh Bhambarkar

Reputation: 928

You should do it as follows:

Service code:

class MyService implements Service {
    private boolean mIsConnectionEstablished = false;

    // Binder given to clients
    private final IBinder mBinder = new LocalBinder();

    public class LocalBinder extends Binder {
        public MyService getService() {
            // Return this instance of LocalService so clients can call public
            // methods
            return MyService.this;
        }
    }

    public interface OnConnectionEstablishedListener {
        public void onConnectionEstablished();
    }

    private OnConnectionEstablishedListener mListener;

    @Override
    public void onCreate() {
       super.onCreate();

       new Thread( new Runnable() {
           @Override
           void run() {
               //Connect to the server here

               notifyConnectionEstablished();
           }
       }).start();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private void notifyConnectionEstablished() {
        mIsConnectionEstablished = true;

        if(mListener != null) {
            mListener.onConnectionEstablished();
        }
    }


    public void setOnConnectionEstablishedListener(
        OnConnectionEstablishedListener listener) {

        mListener = listener

        // Already connected to server. Notify immediately.
        if(mIsConnectionEstablished) {
            mListener.onConnectionEstablished();
        }
    }
}

Activity code:

class MyActivity extends Activity implements ServiceConnection,
    OnConnectionEstablishedListener {

    private MyService mService;
    private boolean mBound;

    @Override
    public void onCreate() {
       super.onCreate();

       //bind the service here
       Intent intent = new Intent(this, MyService.class);
       bindService(intent, this, BIND_AUTO_CREATE);
    }

    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;

        mService.setOnConnectionEstablishedListener(this);
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }

    @Override
    public void onConnectionEstablished() {
        // At this point the service has been bound and connected to the server
        // Do stuff here
        // Note: This method is called from a non-UI thread.
    }
}

Upvotes: 1

Related Questions