线程冻结UI - Android

时间:2017-08-26 15:09:33

标签: java android multithreading

有谁能说这为什么会冻结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()需要一些时间才能完成工作,我使用新线程来阻止用户界面冻结,但由于某种原因它仍会冻结用户界面。

这段代码位于非活动类中,从活动内部从AsyncTask内部调用。

我还试图使用AsyncTask代替线程,而且甚至没有做任何事情(不,我没有忘记执行它。)

所以不知道这段代码有什么问题吗?

1 个答案:

答案 0 :(得分:2)

呀。对run的调用在当前线程上调用Thread的run方法。你想要start()