views:

83

answers:

2

I have an Activity calling a Service defined in IDownloaderService.aidl:

public class Downloader extends Activity {
 IDownloaderService downloader = null;
// ...

In Downloader.onCreate(Bundle) I tried to bindService

Intent serviceIntent = new Intent(this, DownloaderService.class);
if (bindService(serviceIntent, sc, BIND_AUTO_CREATE)) {
  // ...

and within the ServiceConnection object sc I did this

public void onServiceConnected(ComponentName name, IBinder service) {
  Log.w("XXX", "onServiceConnected");
  downloader = IDownloaderService.Stub.asInterface(service);
  // ...

By adding all kinds of Log.xx I found that the code after if(bindService(...)) actually goes BEFORE ServiceConnection.onServiceConnected is being called - that is, when downloader is still null - which gets me into trouble. All the samples in ApiDemos avoid this timing problem by only calling services when triggered by user actions. But what should I do to right use this service after bindService succeeds? How can I wait for ServiceConnection.onServiceConnected being called reliably?

Another question related. Are all the event handlers: Activity.onCreate, any View.onClickListener.onClick, ServiceConnection.onServiceConnected, etc. actually called in the same thread (mentioned in the doc as the "main thread")? Are there interleaves between them, or Android would schedule all events come into being handled one-by-one? Or, When exactly is ServiceConnection.onServiceConnected actually going to be called? Upon completion of Activity.onCreate or sometime when A.oC is still running?

+1  A: 

I did something similar b4, only different is I am not binding to service, but just start it. I would broadcast an intent from the service to notify the caller/activity about it is started.

xandy
Thanks for the quick reply. So in the broadcast receiver you would bind to the service to call the RPC methods, or are you not calling them in your case?
Ryan
You can't bind to a service from a broadcast receiver (since the broadcast receive is done once it returns from onReceive).
hackbod
+3  A: 

How can I wait for ServiceConnection.onServiceConnected being called reliably?

You don't. You exit out of onCreate() (or wherever you are binding) and you put you "needs the connection established" code in onServiceConnected().

Are all the event handlers: Activity.onCreate, any View.onClickListener.onClick, ServiceConnection.onServiceConnected, etc. actually called in the same thread

Yes.

When exactly is ServiceConnection.onServiceConnected actually going to be called? Upon completion of Activity.onCreate or sometime when A.oC is still running?

Your bind request probably is not even going to start until after you leave onCreate(). Hence, onServiceConnected() will called sometime after you leave onCreate().

CommonsWare
Thanks for this information. Hope the Android document could get things clear like this.
Ryan
The various Service API demos have examples; see Local Service Binding and Remote Service Binding in particular: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/index.html also the Service java doc has the sample code for local service binding: http://developer.android.com/reference/android/app/Service.html#LocalServiceSample
hackbod
Thanks hackbod. Apparently this section doesn't appear in my local copy of document. I'll take a look online.
Ryan