Hello, There is a handshake involved when binding to a service. The binding is not successful until the onServiceConnected callback is called. So either send in a callback to initRemoveConnection, or poll the connection checking until twittersendservice is not null.
Hope this helps. Regards Marek On Mar 21, 12:51 am, Jean <[email protected]> wrote: > I have a strange problem and wondering if anyone ran into the same, > and was able to solve it. > > I have a the following class that I use to connect to a service I > created. The catch is that I want to be able to start the service and > use it right away when the user clicks on a button. > > Here is my class: > public class TwitterSendServiceRemoteConnection implements > ServiceConnection { > Context context=null; > private boolean isBound=false; > private ITwitterSendService twittersendservice=null; > > @Override > public void onServiceConnected(ComponentName name, IBinder service) { > twittersendservice = > ITwitterSendService.Stub.asInterface((IBinder) > service); > > SLog.DEBUG_verbose("TwitterSendServiceRemoteConnection(onServiceConnected)"); > } > > @Override > public void onServiceDisconnected(ComponentName name) { > twittersendservice=null; > > SLog.DEBUG_verbose("TwitterSendServiceRemoteConnection(onServiceDisconnected)"); > } > > public TwitterSendServiceRemoteConnection() { > } > > public TwitterSendServiceRemoteConnection(Context c) { > context=c; > } > > public void releaseRemoteConnection() { > if (isBound==true) { context.unbindService(this); } > isBound=false; > } > > public boolean initRemoteConnection(Context c) { > boolean ret=true; > context=c; > if (!isBound) { > Intent i = new Intent(ITwitterSendService.class.getName()); > i.setClassName("com.jeanbombeur.smstoolbox", > com.jeanbombeur.smstoolbox.services.TwitterSendService.class.getName()); > ret = context.bindService(i, this, Context.BIND_AUTO_CREATE); > > SLog.DEBUG_verbose("Verbose:TwitterSendServiceRemoteConnection(initRemoteConnection) > Executed with return code:"+ret); > isBound=ret; > } > return ret; > } > > // From the remote > public boolean testConnection(String text) { > SLog.DEBUG_verbose("Verbose:testconnection before trying > anything"); > try { > return > twittersendservice.testConnection(text); > } catch (RemoteException e) { > // TODO Auto-generated catch block > // e.printStackTrace(); > SLog.DEBUG_verbose("testConnection > error:"+e.getMessage()); > return false; > } catch (Exception e) { > return false; > } > > } > > } > > And the way I use it: > TwitterSendServiceRemoteConnection tssrc=null; > > In my button onClick: > tssrc=new TwitterSendServiceRemoteConnection(context); > tssrc.initRemoteConnection(context); > tssrc.testConnection("Hello World Here"); > > If I initialize my connection early, like when my activity is created, > and only later call my service with testConnection, it all works fine. > > However when I try to initialize and directly use the connection, it > doesn't work. > > It seems that the connection is not finished to be initialized when > the testconnection is called, and the testconnection failed. In my > logs, the "onServiceConnected" is only called -after- the > testConnection call. > > I tried to put a thread sleep but it doesn't change anything... Anyone > knows what is the right way to do that? so I can use my AIDL > connection right after initializing it? -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en To unsubscribe from this group, send email to android-developers+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.

