English 中文(简体)
有关如何在Android上加快速度的任何建议?
原标题:Any advice on how to speed up this in Android?

在一次活动中,我这样做:

  • 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)

这个动作每分钟都在重复进行。

我的问题是:它似乎有点慢?有时当我向下滑动列表时,它很慢?当我点击时,响应不是立即的......可能需要稍微等一下。

你们是否认为我能够用多种“深层”或某种东西来解决这个问题? 请提出咨询意见。 感谢。

编辑:当它在把名字和用户图标加载到列表中时......它实际上是无法使用的。列表会冻结......您无法向上或向下滑动。而当它不加载时......列表滑动得非常缓慢。

问题回答

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!

线程和缓存可能是个好主意。如果你的列表只有十个人(10 × 2kB 的内存,20kB),那么考虑使用缓存列表来更新 UI,在每次下载后更新。请查看引入于 Android 1.5 的 AsyncTask 类。一个简单的代码示例可能如下所示:

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;
}
}

使用缓存列表来处理界面,直到后台任务完成并刷新。您还可以做更高级的事情来节省更多的内存,比如重用refreshlist等,但我希望以上内容提供了如何在后台中完成此操作的一些信息。同时,请记得在列表中重用convertview,以避免在滚动列表时对每个项进行大量的垃圾回收。

除非您设法在一个请求中下载所有信息,否则您可以通过分线程获得时间。另外,重复下载图标真的有必要吗?也许您可以将它们缓存起来?





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签