2017-08-26 95 views
0

有人可以说为什么这会冻结UI线程?线程冻结UI - Android

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,而一个甚至没有做任何事情(不,我还没有忘记去执行它。)

所以任何想法有什么不对的代码?

回答

2

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