0

I'm using AIDL. I have a service and an activity. In the function onCreate I bind the service and I want update the UI:

private ServiceConnection conn = null;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    bindService();
    String name = conn.s.getName();

}
public class ServiceConnection implements ServiceConnection {
public IService s = null;

public void onServiceConnected(ComponentName className, IBinder boundService) {
    s = IService.Stub.asInterface((IBinder) boundService);
    Log.d(getClass().getSimpleName(), "onServiceConnected()");
}

public void onServiceDisconnected(ComponentName className) {
    s = null;
    Log.d(getClass().getSimpleName(), "onServiceDisconnected");
}
}

So I get a nullpointer because the service is not yet bound. How I can check if the activity has bound the service?

Really what I need is easy: create activity -> get info from service using AIDL -> update UI. If I put a button "update" all work fine but I don't know how to do this automatically. Some idea? I could do a thread with:

while(conn.s==null)
  ;
updateUI();

But I think it is not elegant or efficient.

Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
jesuslinares
  • 146
  • 3
  • 11
  • bind service is asynchronous. you can only use it after the connection listener has been notified, that is, in the onConnectionEstablished or something like that. – njzk2 Jan 10 '13 at 16:55
  • you have onServiceConnected that is called when the service is connected. From that method update the UI. Why not? – kingston Jan 10 '13 at 17:30
  • If you really want you can make the onCreate waiting to be notified that the service was bound. But be careful about stopping the UI – kingston Jan 10 '13 at 17:31
  • I can not find anything like "onConnectionEstablished". I cant update the UI from onServiceConnected because the connection is asynchronous. – jesuslinares Jan 10 '13 at 17:40

2 Answers2

0

To test if your service is connected, add simply this method into your ServiceConnection class :

public boolean isConnected() {
   return s == null;
}

An other solution : add a boolean in your class and set to true/false on connect/disconnect.

It's generally a very bad idea to re-use the name of a API class, because it's more difficult to understand which implementations is used before to read all imports.

Kikiwa
  • 1,213
  • 2
  • 13
  • 20
  • But I need update the UI automatically. I need something like a listener that run when the activity has the service bound. I could do: "while(conn.s==null) ;" in a thread but it's not efficient. – jesuslinares Jan 10 '13 at 17:17
0

In the onCreate you should not do things that you can't do before the service is bound. You need to pospone those operations and wait for the onServiceConnected to be called.

Isn't onServiceConnected the "onConnectionEstablished callback" you are looking for? onServiceConnected is called when the connection with the service has been established

kingston
  • 11,053
  • 14
  • 62
  • 116