我已经创建了一个新的应用程序,在开始时有几个后台操作。如果没有其他应用程序在运行,则需要5-6秒来加载应用程序。然而,如果打开其他应用程序,加载时间会更长,需要15-20秒才能加载……有谁知道背后的原因吗?
发布于 2015-06-06 00:58:34
每个应用程序都需要一段时间才能启动,但几秒钟似乎是一个很长的时间,这取决于设备的年龄。您绝对应该尝试将那些长时间运行的操作从UI线程中移除。
如果您不想做任何需要接触UI的事情,可以尝试如下所示:
Runnable runnable = new Runnable() {
@Override
public void run() {
//Do your long-running operations here.
}
};
new Thread(runnable).start();或者,如果你需要做一些涉及UI组件的事情,你可以使用AsyncTasks:
private class LongRunningTask extends AsyncTask<String, Integer, Long> {
protected Long doInBackground(String... data) {
//Do your long-running operations here.
}
protected void onPostExecute(Long result) {
//Update a UI element to show the results
}
}https://stackoverflow.com/questions/30670185
复制相似问题