-1

Every example has Service bind onStart() and calling method from a Service in a while (like after onClick). Until that onClick service is being binded for sure.

I got problem that service is taking a while until it's binded and it binds asynchronously. So if I start and Activity which needs to get some data from a Service right away - it crashes because it's not yet binded. So I need to hold my Activity waiting until it's connection finishes. How do I do that?

2 Answers 2

0

You can leverage a pendingList variable to handle this situation:

private ICoreService mService;
private boolean mConnected = false;
private boolean mConnecting = false;
private List<Something> mPendingTasks = new ArrayList<Something>;

private void bindService() {     
        try {
            Intent service = new Intent();
            service.setClassName(Constants.THE_SERVICE_PACKAGE_NAME, Constants.THE_SERVICE_FULL_NAME);
            mContext.bindService(service, mServiceConnection, Context.BIND_AUTO_CREATE);
            mConnecting = true;
        } catch (Exception e) {
              //handle exception
        }        
}

public void serviceDoSomething(Something task) {
    try {
        if (mService == null) {
            synchronized (mPendingTasks) {
                mPendingTasks.add(task);
            }
        } else {
            mService.doSomething(task);
        }
    } catch (Exception e) {
       //handle exception
    }
}

private ServiceConnection mServiceConnection = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mConnected = false;
        mConnecting = false;
        mService = null;
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mConnected = true;
        mConnecting = false;
        mService = ICoreService.Stub.asInterface(service);
        drainPendingTasks();
    }
};

private void drainPendingTasks() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            synchronized (mPendingTasks) {
                try {
                    if (!mPendingTasks.isEmpty()) {
                        for (Something task : mPendingTasks) {
                            mService.doSomething(task);
                        }
                        mPendingTasks.clear();
                    }
                } catch (Exception e) {
                    //handle exception
                }
            }
        }
    }).start();
}
-1

I think you want that: http://developer.android.com/guide/components/bound-services.html.

On this page you can find:

/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;
    }

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

The onServiceConnected method is called when the service is ready and you can do your stuff inside (or use the mBound attribute).

To bind to your service, use that:

    // Bind to LocalService
    Intent intent = new Intent(this, LocalService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
4
  • yeah, I know that. but if you try to do something like localService.myMethod(); - your activity will crash because binding is not going to be immediatly and if you ask if mBound true or false - it's for sure will be false. What I need is some kind of listener when mBound will be true so Activity can go on Jul 23, 2013 at 9:03
  • You can then put your code directly in onServiceConnected. What's the problem with that ?
    – Brtle
    Jul 23, 2013 at 9:06
  • Great! now that's the answer I was looking for !) I just moved all Activity there Jul 23, 2013 at 11:14
  • well I accepted that, but answer was not on my question . My question was about how to make activity wait for bound Jul 23, 2013 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.