2011-10-06 6 views
2
내가 백그라운드에서 파일을 압축 해제 할 때 AsyncTask를 강제 폐쇄 이유 AsyncTask를 사용하지 않을 때, 그것은 그래서 내가 잘못 뭐하는 거지 모르겠어요 작동 알고

사람 :AsyncTask를 강제 닫기

public class UnzipFile extends AsyncTask<Void, Integer, String> {               
String zipFile = Environment.getExternalStorageDirectory() + "/download/" + mixtapefilename; 
String unzipLocation = Environment.getExternalStorageDirectory() + "/download/"; 

    @Override 
    protected void onPreExecute() {   
     Toast toast = Toast.makeText(getApplicationContext(), "Unzipping...", Toast.LENGTH_SHORT); 
     toast.show(); 

     /* Intent intent = new Intent(); 
     final PendingIntent pendingIntent = PendingIntent.getActivity(Download.this, 0, intent, 0); 

     // configure the notification 
     notification = new Notification(R.drawable.zipicon, "Unzipping...", System 
       .currentTimeMillis()); 
     notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; 
     notification.setLatestEventInfo(getApplicationContext(), "Please Wait", "Unzipping...", pendingIntent); 
     notificationManager2.notify(1, notification);*/ 

    } 

    @Override 
    protected String doInBackground(Void... params) {       
     Decompress d = new Decompress(zipFile, unzipLocation); 
     d.unzip();    

     return "success";}        

    @Override 
    protected void onPostExecute(String result) { 
     Toast toast = Toast.makeText(getApplicationContext(), "Download Complete", Toast.LENGTH_SHORT); 
     toast.show(); 
     File oldzip = new File(zipFile); 
     boolean deleted = oldzip.delete(); 
     sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 
     /*notificationManager2.cancel(1);*/        
    } 
}  

및 여기에 당신이 doInBackground 내부 메인 UI 스레드 (즉, 디스플레이 토스트 메시지)()와 통신 할 수 없습니다 AsynTask을 사용하고 있기 때문에 나는 배경

public class Decompress { 
     byte[] buffer = new byte[1024]; 
     int length; 
     private String _zipFile; 
     private String _location; 

     public Decompress(String zipFile, String location) { 
     _zipFile = zipFile; 
     _location = location; 
     File a = new File(_location + foldername); 
     if(!a.isDirectory()){ 
      a.mkdirs(); 
     }       

     _dirChecker(""); 
     } 

     public void unzip() { 

     try { 
      FileInputStream fin = new FileInputStream(_zipFile); 
      ZipInputStream zin = new ZipInputStream(fin); 
      ZipEntry ze = null; 
      while ((ze = zin.getNextEntry()) != null) { 


      if(ze.isDirectory()) { 
       _dirChecker(ze.getName()); 
      } else { 
       FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
       while ((length = zin.read(buffer))>0) { 
        fout.write(buffer, 0, length); 
        } 

       zin.closeEntry(); 
       fout.close(); 
      } 

      } 
      zin.close(); 
      Toast toast = Toast.makeText(getApplicationContext(),"Download Complete", Toast.LENGTH_LONG); 
      toast.show(); 
     } catch(Exception e) { 
      ProgressDialog dialog; 
     dialog = new ProgressDialog(Download.this);     
     dialog.setMessage(e.toString()); 
     dialog.show(); 
     } 

     } 

     private void _dirChecker(String dir) { 
     File f = new File(_location + dir); 


     if(!f.isDirectory()) { 
      f.mkdirs(); 
     } 
     } 
    } 
+0

logcat이 뱉어내는 예외는 무엇입니까? –

답변

1

에서 실행하려고 내 압축 해제 부분이다.

AsynTask 동안 주요 UI 스레드와 통신 할 수있는 유일한 방법은 onPreExcute() 또는 onPostExcute()을 사용하는 것입니다

그러니이 줄을 제거 :

Toast toast = Toast.makeText(getApplicationContext(),"Download Complete", Toast.LENGTH_LONG); 
+0

게시물을 올린 직후 감사하겠습니다. 무의미한 질문에 사과드립니다. –

0

내가 동의 유감을하지만, 몇 가지 방법이있다 다른 스레드에서 UI 스레드의 코드를 실행하려면 Activity.runOnUiThread (Runnable) View.post (Runnable) View.postDelayed (Runnable, long) 그 중 하나를 사용하여 exe가 될 내용을 게시 할 수 있습니다 현재 스레드에 관계없이 UI 스레드에 의해 cuted하지만 그렇습니다. 대신 예를 들어 Log.d (String, String)를 대신 사용할 수 있습니다.

0

UI 스레드는 스레드로부터 안전하지 않습니다. 따라서 다른 스레드에서 UI 스레드에 액세스하는 것은 매우 위험합니다. View.post() 방법으로 메시지를 메시지 메시지에 게시해야합니다.

관련 문제