我在文件中有1000行,每次加載應用程序時都會向用戶提供這些行。Android字符串內容加載性能
我目前的做法是:
MainActivity:的onCreate:啓動的AsyncTask
的AsyncTask onPreExecute:進度顯示dialiog
的AsyncTask doInBackground:檢查鍵/值存在於共享首選項中,如果是,則doInBackground中不做任何事情。如果沒有(第一次用戶),從原始文件讀取並創建一個stringbuilder。將StringBuilder的內容作爲鍵值對存儲在sharedpreferences中。
AsyncTask onPostExecute:從sharedpreferences填充textview。關閉進度對話框。
的代碼從文件中doInBackground方法讀的是:
StringBuilder sb = new StringBuilder();
InputStream textStream = getBaseContext().getResources().openRawResource(R.raw.file);
BufferedReader bReader = new BufferedReader(new InputStreamReader(textStream));
String aJsonLine = null;
try {
while ((aJsonLine = bReader.readLine()) != null) {
sb.append(aJsonLine + System.getProperty("line.separator"));
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
bReader.close();
textStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我看到的是,用戶必須等待約9-10秒,首次啓動和4-5秒以後啓動。任何建議,以改善我的情況下的表現。
1000行速度快,不需要異步任務 – 2013-03-25 05:52:08
也取決於網絡速度。如果您希望用戶與ui進行交互,則可以使用服務。你可以使用robospice。 – Raghunandan 2013-03-25 05:53:01
@matheszabi,如果我不使用異步任務,UI會掛起10秒鐘。 – Sandeep 2013-03-25 05:54:47