It looks like you are trying to access the connection before the onBind() method is called?
You should NOT be sleeping the thread. You should be returning, and then picking up your work once onBind() is called. You're actually blocking the service from running until you do return, since it needs to run in that same thread. On Apr 5, 2:53 pm, Jason LeBlanc <[email protected]> wrote: > Thanks for the response. I haven't meant to spam the group, I wasn't sure if > my post were posting. > > Problem Activity: LightController > Service: ConnectionService > Object within Service: Connection > > Basically all I am attempting to do is start the Service (from a Splash > Screen Activity) which will provide a Connection to a hardware controller. I > hope to use this Connection to send and receive messages with the hardware > controller. > > I have been able to bind to the Service and send/receive commands with the > controller in the Activity that loads after the Splash Screen. The problem > arose when I tried to create another Activity (LightController) and connect > to the same Service. Since, I'm just learning I assumed I was doing > something wrong with the Service (e.g. binding, unbinding, object sharing, > etc.) > > I finally realized in my LogCat that it was reporting that the Service was > connected after the bit of code I had written to test my connection. I'm > still not sure why this happens. Perhaps something to do with the way these > commands are queued? > > _ begin code snippet from onCreate()_ > > // Bind to the Connection Service > Intent bindIntent = new Intent(LightController.this, > ConnectionService.class); > bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE); > > // Request data from the controller > if (unit = serviceBinder.c.connected() ) { > Log.i(Connection Service is connected);} else { > > Log.i(Connection Service is not connected); > > } > > _ end code snippet from onCreate()_ > > Anyhow, the above if/then would result in a NullPointerException. I > eventually moved it to a try/catch which resulted in a WARN instead of an > ERROR (see LogCat posted above). > > So finally, I have moved the if/then statement to a Thread and all is well. > I sleep for 1000 ms in the Thread prior to attempting to access the > Connection. I am assuming this small time delay is what was needed in order > to establish the binding to the service. Is it that, or are all of the > commands queued until the end of the onCreate() and then executed in some > order determined by Android? (can this be clarified?). > > Additionally, I should probably check to see if the binding has been > completed instead of sleeping the thread for an arbitrary time period. Then > if the service is not bound, sleep for shorter time periods, and then check > again. This may improve the responsiveness of the app. I'm assuming a > _while_ or _for_ loop would be a good method to perform this check. > > Thanks, > J > > On Apr 5, 2010 1:42 PM, "jotobjects" <[email protected]> wrote: > > Certainly not obvious what you are doing or why it doesn't work. Can > you boil this down to a short example that causes the problem so we > can see the code rather than just a long logcat? > > On Apr 5, 8:30 am, Jason LeBlanc <[email protected]> wrote: > > > Is there an obvious reason why ... > > On Apr 4, 2010 9:01 AM, "Tunneling" <[email protected]> wrote: > > > I have a Connection objec... > > [email protected]<android-developers%2Bunsubs > > [email protected]> > > <android-developers%[email protected]<android-developers%252Bu > [email protected]> > > > For more options, visit this group athttp:// > > groups.google.com/group/android-developers?hl=en > > > > > To unsubscribe, reply using "remove me" as the subject. > > -- > > You received this message because you are subscribed to the Google > Groups "Android Developers" group... -- 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

