Thanks for clearing up the onBind(). I have to admit that did throw me
off for a little bit.

I am starting another thread because getUnitProperties() is sending/
receiving messages to the hardware controller to obtain information.
Then it pushes the received data back up to the UI thread. I'm
thinking that qualifies as a background process.

Now it makes sense as to why the Log.i was being displayed AFTER
everything else. The onServiceConnected() call must occur after the
onCreate(), onResume() or onStart() methods? Is that correct?

So Activities (e.g. LightController) used after this can change values
of the Units, and when I return to the main activity, I need to call
getUnitProperties() or some form of it again. Should I "rebind" or is
there a more elegant way to check for the Service prior to calling
getUnitProperties();?

Thank you very much! Now if you'll answer my post on Sockets I'll be
in business :)

Maybe this will make it easier to understand what I'm doing....

http://www.simplycontrolledsystems.com/development.html

On Apr 6, 5:02 pm, Bob Kerns <r...@acm.org> wrote:
> I'm not clear on why you're starting another thread here, but you
> should not be starting it until the service is connected, when your
> onServiceConnected method is called. It's likely it's just a
> workaround, and your call to getUnitProperties() should simply be
> moved to your onServiceConnected() method.
>
> I think I typoed 'onBind' in an earlier message, which probably
> obscured my point (!). onBind() is the service side of the protocol...
>
> That is, your code is just getting lucky right now -- the sleep
> improves the odds, but doesn't make it correct.
>
> Your service is NOT READY until you receive the onServiceConnected
> call. Once you receive that, THEN you can access your service. This
> will NOT OCCUR until AFTER your onCreate() method returns. That is,
> the service will NEVER be ready while you are in onCreate().
>
> What I generally do is to split up my work -- do what doesn't need the
> service in the onCreate() method, and then pick up where I left off in
> the onServiceConnected() method.
>
> First your onCreate() method is called. Then, after that exits, your
> service is started. Once that is set up and initialized and notified
> about the binding, and the main thread is again free, only THEN is
> your onServiceConnected() method called, and only then is it safe for
> your activity to access anything to do with your service.
>
> On Apr 5, 8:18 pm, Jason LeBlanc <jasonalebl...@gmail.com> 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 
> > <jasonalebl...@gmail.com>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 <r...@acm.org> 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 <jasonalebl...@gmail.com> 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" <jotobje...@gmail.com> 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 <jasonalebl...@gmail.com> wrote:
>
> > >> > > Is there an obvious reason why ...
> > >> > > On Apr 4, 2010 9:01 AM, "Tunneling" <jasonalebl...@gmail.com> wrote:
>
> > >> > > I have a Connection objec...
> > >> > > android-developers+unsubscr...@googlegroups.com<android-developers%2Bunsubs
> > >> > >  cr...@googlegroups.com><android-developers%2Bunsubs
> > >> cr...@googlegroups.com>
>
> > >> > <android-developers%2bunsubscr...@googlegroups.com<android-developers%252Bu
> > >> >  nsubscr...@googlegroups.com><android-developers%252Bu
> > >> nsubscr...@googlegroups.com>
>
> > >> > > 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 android-developers@googlegroups.com
> > >> To unsubscribe from this group, send email to
> > >> android-developers+unsubscr...@googlegroups.com<android-developers%2Bunsubs
> > >>  cr...@googlegroups.com>
> > >> 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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to