谁能说为什么这会冻结UI线程?
new Thread(new Runnable() {
@Override
public void run() {
try {
if (!downloadingdemo) {
if (downloadlink.indexOf(".zip") > -1) {
File zipped = new File(context.getExternalFilesDir(context.getFilesDir().getAbsolutePath()).getAbsolutePath() + "/cctemp/books/" + bookid.toString() + ".czfile");
ZipFile zipFile = new ZipFile(zipped);
try {
//File src = new File(basedownloadlocation + bookid + ".czfile");
//zipFile = new ZipFile(src);
if (zipFile.isEncrypted()) {
zipFile.setPassword("www.comic-city.ir");
}
} catch (ZipException e1) {
e1.printStackTrace();
}
File f = new File(basedownloadlocation + bookid);
if (f.exists()) {
for (int i = 0; i < f.listFiles().length; i++) {
File[] lists = f.listFiles();
File fs = lists[0];
fs.delete();
}
f.delete();
}
f.mkdir();
zipFile.extractAll(basedownloadlocation + bookid);
File f1 = new File(basedownloadlocation + bookid + ".czfile");
if (f1.exists()) {
File f2 = new File(basedownloadlocation + bookid);
if (f2.exists()) {
for (int i = 0; i < f2.listFiles().length; i++) {
File f3 = new File(basedownloadlocation + bookid + ".cdfile");
if (f3.exists())
f3.delete();
File[] files = f2.listFiles();
File extractedfile = files[0];
extractedfile.renameTo(new File(basedownloadlocation + bookid + ".cdfile"));
}
f2.delete();
f1.delete();
Encryption.EncryptComic(basedownloadlocation + bookid + ".cdfile", basedownloadlocation + bookid + ".cfile");
//File_Encryption.EncryptFile(basedownloadlocation + bookid + ".cefile", basedownloadlocation + bookid + ".cfile");
File f4 = new File(basedownloadlocation + bookid + ".cdfile");
f4.delete();
}
}
} else {
File f = new File(basedownloadlocation + bookid + ".czfile");
f.renameTo(new File(basedownloadlocation + bookid + ".cfile"));
}
}
}catch(Exception e){}
}).run();
每一行都工作正常,它只是冻结 UI。zipFile.extractAll()
和Encryption.EcnryptComic()
行需要一些时间来完成它们的工作,我正在使用一个新线程来防止 UI 冻结,但由于某种原因它仍然冻结了 UI。
此代码块位于非活动类中,该类从活动内部从 AsyncTask 内部调用。
我还尝试使用 AsyncTask 代替线程,那个线程甚至什么都不做(不,我没有忘记执行它。
所以知道这段代码有什么问题吗?
是的。 对run
的调用在当前线程上调用线程的run
方法。 你想要start()
.