2017-08-26 93 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()