Java 线程冻结用户界面-Android
有人能说为什么会冻结UI线程吗Java 线程冻结用户界面-Android,java,android,multithreading,Java,Android,Multithreading,有人能说为什么会冻结UI线程吗 new Thread(new Runnable() { @Override public void run() { try { if (!downloadingdemo) { if (downloadlink.indexOf(".zip") > -1) {
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();
new线程(new Runnable()){
@凌驾
公开募捐{
试试{
如果(!下载演示){
if(downloadlink.indexOf(“.zip”)>-1){
文件压缩=新文件(context.getExternalFilesDir(context.getFilesDir().getAbsolutePath()).getAbsolutePath()+“/cctemp/books/”+bookid.toString()+”.czfile”);
ZipFile ZipFile=新ZipFile(拉链式);
试一试{
//File src=新文件(basedownloadlocation+bookid+“.czfile”);
//zipFile=新zipFile(src);
if(zipFile.isEncrypted()){
设置密码(“www.comic-city.ir”);
}
}捕获(ZipException e1){
e1.printStackTrace();
}
文件f=新文件(basedownloadlocation+bookid);
如果(f.exists()){
对于(int i=0;i
每一行都可以正常工作,只是冻结了UI。这些行zipFile.extractAll()
和Encryption.EcnryptComic()
需要一些时间来完成它们的工作,我正在使用一个新线程来防止UI冻结,但出于某种原因它仍然冻结了UI
此代码块位于非活动类中,该类从活动内部的AsyncTask内部调用
我也尝试过使用AsyncTask而不是线程,而且它甚至不做任何事情(不,我没有忘记执行它)
知道这段代码有什么问题吗?是的。调用
run
在当前线程上调用线程的run
方法。你想要start()是的。调用run
在当前线程上调用线程的run
方法。你想要start()