0

I'm calling an AIDL service from within an IntentService. Once binding to the AIDL service is done, the IntentService must pass a parameter to the AIDL service for execution.

Now since binding to the AIDL takes takes place asynchornously, I want the code in IntentService to wait it out, before passing the parameter to the AIDL service.

pseudoCode:

STEP 1: Bind to AIDL service

STEP 2: Pass parameter to AIDL service

In other words, STEP2 is reached even before STEP 1 completes, coz binding to AIDL is asynchronous. Then I get exceptions.

How do I wait for STEP 1 to complete, before STEP 2 executes ?

Abhishek
  • 743
  • 1
  • 15
  • 28

1 Answers1

0

You muse use ServiceConnection.onServiceConnected() to monitor connection to service.Once the binding is complete you will get callback in onServiceConnected

Below is documentation snippet present in Android developers guide

A client can bind to the service by calling bindService(). When it does, it must provide an implementation of ServiceConnection, which monitors the connection with the service. The bindService() method returns immediately without a value, but when the Android system creates the connection between the client and service, it calls onServiceConnected() on the ServiceConnection, to deliver the IBinder that the client can use to communicate with the service.

Vipul
  • 27,808
  • 7
  • 60
  • 75
  • Hi Vipul. I set a boolean flag at the end of the onServiceConnected() method. Before calling STEP 2, I put an empty loop while (!flag) {}. But the emulator hangs because of this. – Abhishek Oct 08 '13 at 10:03
  • Should I use **AsyncTask** ? I'm just worried about the complexity. – Abhishek Oct 08 '13 at 10:14