Android Loading list items from service results in hover

In my Android app, I have a ListActivity. This ListActivity uses a SimpleAdapter that I populate with items from my service. So, in my code, I:

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

      

This whole operation runs at almost any time (less than a second), so I want it to run on the UI thread. See my onCreate:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    fillDataUsingCodeAbove();
}

      

The reason I want this to run on the UI thread is because if you have a list item selected or scroll to a specific position in the ListView, you rotate the device or take out the keyboard or something like that cause a change config), when my activity is restarted Android will try to restore the state immediately after onCreate. But, if I run it on a separate thread, it doesn't happen. There is also a cool fadein animation :)
The problem I ran into running it on the UI thread is that when I try to bind to a service, this service bind request is put on the message queue. But then when I go into my loop, I stop the message queue from looping. So my program hangs because it expects the service to bind and the service will not bind until the end of the loop. I was thinking about placing Looper.loop()

inside my loop, but it just hangs in Looper.loop()

(I don't know why.)
Sorry for such a long question,
Isaac Waller

0


a source to share


1 answer


I think you should be using AsyncTask , it won't localize the UI thread, and it won't be much more complex than what you already have.



0


a source







All Articles