How to bind to a running Android service?

I hope this is more of a code problem than anything else, and I hope that someone out there can help fix the problem.

I have other code that starts the service using startService (), and I can verify that the service is running when the debugger calls the onCreate () DecoderService function.

However, bindService never communicates with a running service. Is this an asynchronous call, and do I need to wait for something?

public class ResultsActivity extends Activity { protected void onResume() { // TODO Auto-generated method stub super.onResume(); Intent decoderIntent = new Intent(this,DecoderService.class); _isBound = bindService(decoderIntent,mConnection,Context.BIND_AUTO_CREATE); _textView.start(_mBoundService); } private boolean _isBound = false; private DecoderService _mBoundService; private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { _mBoundService = ((DecoderService.LocalBinder)service).getService(); } public void onServiceDisconnected(ComponentName className) _mBoundService = null; } }; } public class DecoderService extends Service { protected void onHandleIntent(Intent intent) { // TODO Auto-generated method stub _numCompleted = 5; _numTotal = 100; } protected int _numCompleted = 0; protected int _numTotal = 0; public void onCreate() { onHandleIntent(null); } @Override public IBinder onBind(Intent intent) { return mBinder; } // This is the object that receives interactions from clients. See // RemoteService for a more complete example. private final IBinder mBinder = new LocalBinder(); public class LocalBinder extends Binder { DecoderService _ref = null; public LocalBinder() { _ref = DecoderService.this; } DecoderService getService() { return DecoderService.this; } } } 
+1
android android-activity service
Jul 28 '10 at 23:03
source share
1 answer

This is an asynchronous call, but will I have to wait for something to complete?

The binding is not ready until onServiceConnected() calls the onServiceConnected() object.

+4
Jul 29 '10 at 1:53 on
source share



All Articles