在doInBackground完成之前调用onPostExecute-异步任务 [英] onPostExecute called before doInBackground completes - Async task

查看:465
本文介绍了在doInBackground完成之前调用onPostExecute-异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的方法:

public void extractFiles() {

    AsyncTask<Void, Void, Boolean> extractionTask = new AsyncTask<Void, Void, Boolean>() {

        @Override
        protected void onPreExecute() {
        progressDialog = new ProgressDialog(Activity.this);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("Extracting Files Please wait...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setProgress(0);
        progressDialog.show();
        super.onPreExecute();
        }

        @Override
        protected Boolean doInBackground(Void... params) {
        // TODO Auto-generated method stub
        String xapkFilePath = XAPKFilePath(Activity.this);
        String exportDirectory = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/"
            + Activity.this.getPackageName() + "/files/";
        File exportDirectoryFilepath = new File(exportDirectory);
        exportDirectoryFilepath.mkdirs();
        ZipHelper zhelper = new ZipHelper();


        System.out.println("In background called");
        zhelper.unzip(xapkFilePath, exportDirectoryFilepath);

        return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            System.out.println("progress dialog dismissed");
        }
        if (result) {
            //start intent.
        }
        }

    };
    extractionTask.execute();
    }

public class ZipHelper {

    boolean zipError = false;

    public boolean isZipError() {
    return zipError;
    }

    public void setZipError(boolean zipError) {
    this.zipError = zipError;
    }

    public void unzip(String archive, File outputDir) {
    try {
        Log.d("control", "ZipHelper.unzip() - File: " + archive);
        ZipFile zipfile = new ZipFile(archive);
        for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
        ZipEntry entry = (ZipEntry) e.nextElement();

        System.out.println("OUTPUT DIR 1*" + outputDir);
        System.out.println("ENTRY IS " + entry);

        unzipEntry(zipfile, entry, outputDir);

        }
    } catch (Exception e) {
        Log.d("control", "ZipHelper.unzip() - Error extracting file " + archive + ": " + e);
        setZipError(true);
    }
    }

    private void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {
    if (entry.isDirectory()) {
        createDirectory(new File(outputDir, entry.getName()));
        return;
    }

    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDirectory(outputFile.getParentFile());
        System.out.println("OUTPUT FILE IS " + outputFile.getParentFile());
    }

    Log.d("control", "ZipHelper.unzipEntry() - Extracting: " + entry);
    BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
    try {
        IOUtils.copy(inputStream, outputStream);
    } catch (Exception e) {
        Log.d("control", "ZipHelper.unzipEntry() - Error: " + e);
        setZipError(true);
    } finally {
        outputStream.close();
        inputStream.close();
    }
    }

    private void createDirectory(File dir) {
    Log.d("control", "ZipHelper.createDir() - Creating directory: " + dir.getName());
    if (!dir.exists()) {
        if (!dir.mkdirs()) {
        throw new RuntimeException("Can't create directory " + dir);
        }
    } else {
        Log.d("control", "ZipHelper.createDir() - Exists directory: " + dir.getName());
    }
    }

}

我在这里调用这样的方法extractFiles(),但是发生的事情甚至是在doInBackground完成之前,即提取我正在显示微调器的文件,调用onPostExecute并移至下一个屏幕.

Here i call the method like this extractFiles() but what is happening is even before the doInBackground is completed that is extracting the files for which i am showing a spinner , the onPostExecute is called and moves to next screen.

这是怎么了?

推荐答案

在doinbackground中检查您的状况,如果执行,则是否完全执行您的操作,否则返回true,

check your condition in doinbackground completely perform your operation or not if perform then return true,

zhelper.unzip(xapkFilePath, exportDirectoryFilepath);

这篇关于在doInBackground完成之前调用onPostExecute-异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆