2012-08-12 6 views
0

아래 코드를 실행하여 750 파일이 포함 된 70MB Zip 파일을 압축하는 동안 장치가 응답하지 않고 거의 충돌합니다. 누구는 내게 무슨 잘못을 말할 수 :장치가 응답하지 않게 만드는 과정을 압축 해제

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; 
} 

새로운 코드 : 둘 이상의 100 UpdatePercentNotificationBar 호출하는 코드의 일부에서

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

+0

@CommonsWare이 코드는 IntentService에서 실행 중입니다. 거기에 아무것도 잘못 될 것/위의 코드에서 향상시킬 수 있습니까? 전체 기능으로 코드를 업데이트했습니다. 알림을 업데이트하는 것을 포함합니다. – Omar

+0

추가 테스트를하는 동안 "시스템 UI 작동이 중지되었습니다"!! – Omar

답변

1

times는 prev에 float 값을 사용하므로 모든 루프가 새 메시지를 생성합니다.

원본 :

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

같은 것을해야합니다

당신이 더 이상 메시지와 UI를 범람하는이 논리를 변경하여 확인 된
int prev; 
... 
if (prev != (int)(current/finalSize * 100)) { 
    prev = (int)(current/finalSize * 100); 
    UpdatePercentNotificationBar(prev); 
} 

.