It also doesn't work if I remove the Thread.sleep(). On Mon, Apr 5, 2010 at 10:18 PM, Jason LeBlanc <[email protected]>wrote:
> Bob, am I taking the wrong approach here? I'm hardly satisfied that it's > just "working". I'd like to fully understand what's going on here. > > Thanks, > J > > > On Mon, Apr 5, 2010 at 8:32 PM, Jason LeBlanc <[email protected]>wrote: > >> Here is my onCreate(), that works: >> =========================== >> >> // Called at the start of the full lifetime. >> @Override >> public void onCreate(Bundle savedInstanceState) { >> super.onCreate(savedInstanceState); >> setContentView(R.layout.light_control); >> >> // Initialize activity. >> >> // Obtain the Unit ID from the intent >> Bundle extras = getIntent().getExtras(); >> unitId = extras.getInt("com.scs.haus.UNIT_ID", 0); >> >> // Bind to the Connection Service >> >> Intent bindIntent = new Intent(LightController.this, >> ConnectionService.class); >> bindService(bindIntent, mConnection, >> Context.BIND_AUTO_CREATE); >> >> viewUnitProperties = new Runnable(){ >> @Override >> public void run() { >> try { >> Thread.sleep(1000); >> } catch (InterruptedException e) { >> Log.e("SLEEP","I CANT SLEEP"); >> } >> >> getUnitProperties(); >> } >> }; >> Thread thread = new Thread(null, viewUnitProperties, >> "ViewUnitPropertiesThread"); >> thread.start(); >> } >> =========================== >> standard binder stuff >> =========================== >> // Handles the connection between the service and activity >> private ServiceConnection mConnection = new ServiceConnection() { >> public void onServiceConnected(ComponentName className, >> IBinder service) { >> // Called when the connection is made >> serviceBinder = >> ((ConnectionService.MyBinder)service).getService(); >> Log.i("LightController","Service Connected"); >> } >> >> public void onServiceDisconnected(ComponentName className) { >> // Received when the service unexpectedly disconnects >> serviceBinder = null; >> Log.i("LightController","Service Disconnected"); >> } >> }; >> =========================== >> >> If I try to run the getUnitProperties() method outside of the Thread, I >> get a NullExceptionPointer AND the Log.i for onServiceConnected() shows up >> in the log after the exception. >> >> Thanks, >> J >> >> >> On Mon, Apr 5, 2010 at 8:08 PM, Bob Kerns <[email protected]> wrote: >> >>> 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%[email protected]><android-developers%2Bunsubs >>> [email protected]> >>> > >>> > <android-developers%[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]<android-developers%[email protected]> >>> For more options, visit this group at >>> http://groups.google.com/group/android-developers?hl=en >>> >> >> > -- 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

