gpt4 book ai didi

Android 应用程序有时加载时间过长

转载 作者:行者123 更新时间:2023-11-30 02:05:54 24 4
gpt4 key购买 nike

我创建了一个新的应用程序,它在启动时有多个后台操作。如果没有其他应用程序在运行,则加载该应用程序需要 5-6 秒。但是如果打开其他应用程序,加载时间会更长,需要15-20秒才能加载...有谁知道背后的原因吗?

最佳答案

每个应用程序都需要一段时间才能启动,但几秒钟似乎很长,具体取决于设备的年龄。您一定要尝试让那些长时间运行的操作脱离 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
}
}

关于Android 应用程序有时加载时间过长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30670185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com