2012-08-12 136 views
0

運行下面的代碼解壓縮70 MB的壓縮文件(其中包含750個文件)時,該設備變得無響應,幾乎崩潰。誰能告訴我什麼是錯的:解壓過程使設備無響應

boolean UNZipFiles() { 
    float prev = -1; // to check if the percent changed and its worth updating the UI 
    int finalSize = 0; 
    float current = 0; 

    try { 
     final int BUFFER = 2048; 
     String zipFilePath = PATH + FileName; 

     BufferedOutputStream dest = null; 
     BufferedInputStream is = null; 
     ZipEntry entry; 
     ZipFile zipfile = new ZipFile(zipFilePath); 

     finalSize = (int) new File(zipFilePath).length(); 

     Enumeration<? extends ZipEntry> e = zipfile.entries(); 
     while (e.hasMoreElements()) { 
      entry = e.nextElement(); 
      current += entry.getCompressedSize(); 

      if (entry.isDirectory()) 
       dirChecker(entry.getName()); 
      else { 
       int count; 
       byte data[] = new byte[BUFFER]; 

       is = new BufferedInputStream(zipfile.getInputStream(entry)); 
       FileOutputStream fos = new FileOutputStream(PATH + entry.getName()); 
       dest = new BufferedOutputStream(fos, BUFFER); 
       while ((count = is.read(data, 0, BUFFER)) != -1) 
        dest.write(data, 0, count); 

       if (prev != current/finalSize * 100) { 
        prev = current/finalSize * 100; 

        UpdatePercentNotificationBar((int) prev); 
       } 

       dest.flush(); 
       dest.close(); 
       is.close(); 
      } 
     } 


     DeleteZip(zipFilePath); 

     success = true; 
    } catch (Exception e) { 
     NotificationBarFail("Error while Downloading"); 
     return false; 
    } 

    return true; 

} 

通知管理:

public void onCreate() {   
    super.onCreate(); 
    mContext = this; 

    Intent intent = new Intent(this, Main.class); 
    pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); 
    notification = new Notification(R.drawable.icon, "Downloading files", System.currentTimeMillis()); 
    notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; 
    notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.background_service); 
    notification.contentIntent = pendingIntent; 
    notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon); 
    notification.contentView.setTextViewText(R.id.status_Percentage, "0%"); 
    notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false); 

    notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE); 

    notificationManager.notify(42, notification); 
    FileName = Main.Main_File_Name; 
} 

新代碼:

boolean UNZipFiles() { 
     byte[] buffer = new byte[4096]; 
     int length; 
     float prev = -1; // to check if the percent changed and its worth updating the UI 
     int finalSize = 0; 
     float current = 0; 

     try { 

      String zipFile = PATH + FileName; 

      FileInputStream fin = new FileInputStream(zipFile); 
      ZipInputStream zin = new ZipInputStream(fin); 

      finalSize = (int) new File(zipFile).length(); 

      ZipEntry ze = null; 

      while ((ze = zin.getNextEntry()) != null) { 
       current += ze.getCompressedSize(); 
       if (ze.isDirectory()) 
        dirChecker(ze.getName()); 
       else { 
        FileOutputStream fout = new FileOutputStream(PATH + ze.getName()); 
        while ((length = zin.read(buffer)) > 0) 
         fout.write(buffer, 0, length); 
        if (prev != current/finalSize * 100) { 
         prev = current/finalSize * 100; 

         UpdatePercentNotificationBar((int) prev); 
        } 
        zin.closeEntry(); 
        fout.close(); 
       } 

      } 
      zin.close(); 
      DeleteZip(zipFile); 


      success = true; 
     } catch (Exception e) { 
      NotificationBarFail("Error while downloading"); 
      return false; 
     } 

     return true; 

    } 
+0

也許你正在主應用程序線程上調用此代碼。 – CommonsWare 2012-08-12 13:46:16

+0

@CommonsWare此代碼正在IntentService中運行。在上面的代碼中似乎沒有任何錯誤/可以改進?我更新了整個功能的代碼,它包括更新通知 – Omar 2012-08-12 14:12:56

+0

進一步測試時,我得到了「系統UI停止工作」! – Omar 2012-08-12 14:32:38

回答

1

在你的代碼,你在呼喚UpdatePercentNotificationBar超過100個零件因爲您正在使用prev的浮點值,所以每個循環都會生成一條新消息。

原文:

float prev; 
... 
if (prev != current/finalSize * 100) { 
    prev = current/finalSize * 100; 
    UpdatePercentNotificationBar((int) prev); 
} 

應該是這樣的:

int prev; 
... 
if (prev != (int)(current/finalSize * 100)) { 
    prev = (int)(current/finalSize * 100); 
    UpdatePercentNotificationBar(prev); 
} 

當你改變這個邏輯,你不再充斥消息UI證實。