AsyncTask强制关闭

时间:2011-10-06 00:19:25

标签: android android-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);*/                             
    }
}       

然后这是我尝试在后台运行的解压缩部分

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

3 个答案:

答案 0 :(得分:1)

由于您使用的是AsynTask,因此您无法与doInBackground()内的主UI线程(即显示Toast消息)进行通信。

在AsynTask期间与主UI线程通信的唯一方法是使用onPreExcute()onPostExcute()

所以只需删除此行:

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

答案 1 :(得分:0)

我很抱歉不同意但是有几种方法可以在另一个线程的UI线程中执行代码:     Activity.runOnUiThread(可运行)     View.post(可运行)     View.postDelayed(Runnable,long) 您可以使用其中一个来发布将由UI线程执行的内容,而不管您当前所在的线程。但是,您可以将它们取消并使用Log.d(字符串,字符串)代替(例如)

答案 2 :(得分:0)

UI线程不是线程安全的。因此,在其他线程中访问UI Thread非常危险。您必须按View.post()方法

将消息发布到UI消息队列