Isaac Waller wrote:
> In my Android application, I have a ListActivity. This ListActivity
> uses a SimpleAdapter that I fill with items from my service. So, in my
> code, I do:
>
> MySuperCoolService.Binder serviceBinder = null;
> private ServiceConnection serviceConnection = new ServiceConnection()
> {
> public void onServiceConnected(ComponentName className,
> IBinder service) {
> Log.d(TAG, "Service connection: connected!");
> serviceBinder = (MySuperCoolService.Binder)service;
> }
> public void onServiceDisconnected(ComponentName className) {
> Log.d(TAG, "Service connection: disconnected");
> serviceBinder = null;
> }
> };
> bindService(new Intent(this, MySuperCoolService.class),
> serviceConnection, BIND_AUTO_CREATE);
> while(serviceBinder==null) {
> Thread.Sleep(1000);
> }
> // now retrieve from service using binder and set list adapter
Icky!
Move the "now retrieve from service using binder and set list adapter"
into onServiceConnected() and get rid of the sleep() infinite loop.
> This whole operation takes hardly any time (less than a second), so I
> want it to run in the UI thread.
Is your service positively deterministic, such that it will *always*
take less than a second? Or does it take less than a second only in
normal conditions (e.g., connectivity is OK to some server you're
fetching data from)?
> The reason I want this to run in the UI thread is that if you have a
> list item selected, or you have scrolled to a certain position in the
> ListView, and you rotate the device or take out the keyboard or
> something (to trigger a configuration change) when my activity is
> restarted, Android will try to restore the state right after onCreate.
By default, yes. There are other ways of dealing with this.
> But, if I run it in a separate thread, it will not.
Sure it can. Either don't reload the state from the service (by using
onRetainNonConfigurationInstance() and having your state persist across
rotations), or fork another background thread (if these are
load-data-and-close threads), or tell a persistent background thread
"yo! load the stuff again!".
> The problem I am having with running it in the UI thread is that when
> I try to bind to the service, that service bind request gets put onto
> the message queue.
Correct.
> But then when I go into my loop, I stop the message
> queue from looping.
Equally correct.
> So my program hangs, because it's waiting for the
> service to get bound, and the service won't get bound until the loop
> ends (I think you call this a deadlock).
Spot on! In other words, icky!
> Sorry for such a long question,
I seem to have missed the question.
If the question is "how do I get rid of the infinite loop?", move the
"now retrieve from service using binder and set list adapter" into
onServiceConnected().
However, unless you're really really certain this will always occur very
very quickly, use a background thread, in addition to getting rid of the
infinite loop.
--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy
_The Busy Coder's Guide to Android Development_ Version 2.0 Available!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---