Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any advice on how to speed up this in Android?

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.

like image 669
TIMEX Avatar asked Feb 28 '23 13:02

TIMEX


2 Answers

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!

like image 99
Zordid Avatar answered Mar 05 '23 04:03

Zordid


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.

like image 35
BMB Avatar answered Mar 05 '23 04:03

BMB