views:

133

answers:

3

In one Activity, I am doing this:

  • Every minute, I update the GPS location to the cloud.
  • Then, after the location is updated, I download a list of 10 people and their icons...and update them in the list. (each icon is 80x80 and about 2Kb)

This is done every minute, over and over.

My problem is: It seems to be a little slow? Sometimes when I swipe down the list, it's slow? And when I click, the response is not immediate...and may require a little hold.

Do you think I can fix this with multiple "threads" or something? Please advise. Thanks.

Edit: When it's loading the names and user-icons into the list...it's practically unusable. The list freezes...and you can't swipe up or down. And when it's not loading ...the list swipes very slowly.

+1  A: 

Unless you manage to download all information in one request, you will gain time by threading it. Also, is it really necessary to download the icons over and over again? Maybe you can cache them?

sandis
How do I cache them?
TIMEX
well, you could simply write them to the sd-card or something. The problem really depends on how you can detect if an icon needs to be updates for some reason.
sandis
+4  A: 

For actions like downloading stuff "in" an Activity it is always useful to have background services to do the work for the Activity that then stays responsive! If you try to do stuff like that within the Activity itself, you definitely get in trouble with responsiveness, I can assure you! So: my answer is - yes. Implement a background service for that!

Zordid
A service is the way to go, an you can still add caching to the service if it makes sense.
vickirk
+2  A: 

Threading and caching are probably good ideas. If your list is only ten people (10 times 2kB of memory, 20 kB) then consider using a cached list for the UI and update it after each download. Check the AsyncTask class that was introduced with Android 1.5 for this. A simple code sample could look something like this:

mCachedList;

private void onUpdate(Location location) { 
  DownloadTask refresh = new DownloadTask(location);
  refresh.execute();
}

private class DownloadTask extends AsyncTask<Location, void, ArrayList<ListItem> {

protected ArrayList<ListItem> doInBackground(Location... params) {
  final ArrayList<ListItem> refreshlist = new ArrayList<ListItem>;
  Location location = params[0];
  sendToCloud(location);
  while(!done) {
    ListItem next = downLoadPerson();
    refreshlist.add(next);
  }
  return refreshlist;
}

protected void onPostExecute(ArrayList<ListItem> refreshlist) {
  mCachedList = refreshlist;
}
}

Use the cached list for the UI until the background task finishes and it is refreshed. There are more advanced things you could do to save more memory like reusing the refreshlist etc but I hope the above provides some info on how to do this in the background. Also remember to reuse the convertview in your list to avoid a lot of garbage collection on each item when you are scrolling in the list.

BMB