我正在创建一个应用程序,该应用程序涉及从 Assets 文件夹中的文本文件中读取数据。对于每个文件,它将数据存储在单独的ArrayList
. 这些文件在活动的onCreate()方法中一个接一个地读取。所有文本文件加起来总共 1.8 MB,在模拟器上,加载活动当前需要 12 秒。该应用程序不会在模拟器上崩溃(只需大约 12 秒)。
我读过关于异步线程的文章,但过去我从来没有需要它们。我正计划使用某种消息或进度条来通知用户活动实际上正在加载并且没有崩溃。
所以我的问题是:即使应用程序在加载活动时没有崩溃,我是否仍然应该将文件的读取放在异步或不同的线程上?如果是这样,我将如何正确地做到这一点?(我以前从未这样做过。)
这是读取文本文件的示例代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
populateArrayLists();
}
public void populateArrayLists() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(
"text1.txt")));
String text;
while ((word = br.readLine()) != null) {
ArrayList1.add(text);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close(); // stop reading
} catch (IOException ex) {
ex.printStackTrace();
}
}
br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(
"text2.txt")));
// the same process is duplicated for 1-15
// it may not be the best or most efficient way but it works
任何帮助,将不胜感激。