I am wondering if anyone has a similar problem.

I have an application I am developing that make use of AIDL for remote
call to a service. I create an abstraction class for that so I can re-
use it anywhere.

That class takes care of all the initialization:

public class TwitterSendServiceRemoteConnection implements
ServiceConnection {
        Context context=null;
        private boolean isBound=false;
        private ITwitterSendService twittersendservice=null;

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
                // TODO Auto-generated method stub
                twittersendservice = 
ITwitterSendService.Stub.asInterface((IBinder)
service);
                SLog.DEBUGmakeToast(context,
"TwitterSendServiceRemoteConnection(onServiceConnected)");
        
SLog.DEBUG_verbose("TwitterSendServiceRemoteConnection(onServiceConnected)");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
                // TODO Auto-generated method stub
                twittersendservice=null;
                SLog.DEBUGmakeToast(context,
"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) {
                                SLog.DEBUG_verbose("testConnection 
error:"+e.getMessage());
                                return false;
                        } catch (Exception e) {
                                return false;
                        }
        }
}

This works great if when in my other activity or service I initialize
it during the onCreate action:

TwitterSendServiceRemoteConnection tssrc=null;

                tssrc=new TwitterSendServiceRemoteConnection(context);
                tssrc.initRemoteConnection(context);

Then when I call a test function from my class from a button, or
onclick, later, it works fine.

However. When I perform the following for example on click:

1.                              tssrc=new 
TwitterSendServiceRemoteConnection(context);
2.                              tssrc.initRemoteConnection(context);
3.                              tssrc.testConnection("Hello World Here");

3. seems to get called too early.

In my logs what shows is
1. initRemoteConnection is executed successfully
2. testconnection is executed but fails because the connection isn't
ready
3. call to onServiceConnected is done

Any way to solve this? I tried to put a Thread.sleep before 2 and 3
but it doesn't matter how long, it doesn't change a thing.

-- 
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.

Reply via email to