1

我遇到了这个问题,我挣扎了几个小时,我认为这需要几分钟。
我尝试了很多东西,比如使用线程等,但我认为这可以更容易完成。

这是我的解压类(Unzipper):

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Enumeration;
    import java.util.Observable;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;

    import org.apache.commons.io.IOUtils;

    import android.os.AsyncTask;
    import android.os.Environment;
    import android.util.Log;

    public class Decompress extends Observable {

        static final String TAG = "UnZip";
        int counter;
        private String mFileName, mFilePath, mDestinationPath;

        public Decompress (String fileName, String filePath, String destinationPath) {
            mFileName = "bigflow";
            mFilePath = Environment.getExternalStorageDirectory().toString();
            mDestinationPath = Environment.getExternalStorageDirectory() + "/bigflow/";

        }

        public String getFileName () {
            return mFileName;
        }

        public String getFilePath() {
            return mFilePath;
        }

        public String getDestinationPath () {
            return mDestinationPath;
        }

        public void unzip () {
            System.out.println("unzipping...");
            String fullPath = mFilePath + "/" + mFileName + ".zip";
            Log.d(TAG, "unzipping " + mFileName + " to " + mDestinationPath);
            new UnZipTask().execute(fullPath, mDestinationPath);

        }

        private class UnZipTask extends AsyncTask<String, Void, Boolean> {

            @SuppressWarnings("rawtypes")
            @Override
            protected Boolean doInBackground(String... params) {
                String filePath = params[0];
                String destinationPath = params[1];

                File archive = new File(filePath);
                try {
                    ZipFile zipfile = new ZipFile(archive);
                    for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
                        ZipEntry entry = (ZipEntry) e.nextElement();
                        unzipEntry(zipfile, entry, destinationPath);
                    }
                } catch (Exception e) {
                    Log.e(TAG, "Error while extracting file " + archive, e);
                    return false;
                }

                return true;
            }

            @Override
            protected void onPostExecute(Boolean result) {
                setChanged();
                notifyObservers();
            }

            private void unzipEntry(ZipFile zipfile, ZipEntry entry,
                    String outputDir) throws IOException {

                if (entry.isDirectory()) {
                    createDir(new File(outputDir, entry.getName()));
                    return;
                }

                File outputFile = new File(outputDir, entry.getName());
                if (!outputFile.getParentFile().exists()) {
                    createDir(outputFile.getParentFile());
                }

                Log.v(TAG, "Extracting: " + entry);
                BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
                BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

                try {
                    IOUtils.copy(inputStream, outputStream);
                } finally {
                    outputStream.close();
                    inputStream.close();
------------------->   counter++; <--------------------------------
                }

               } 

            private void createDir(File dir) {
                if (dir.exists()) {
                    return;
                }
                Log.v(TAG, "Creating dir " + dir.getName());
                if (!dir.mkdirs()) {
                    throw new RuntimeException("Can not create dir " + dir);
                }
            }

        }
    } 

counter++;我想检查它是否在 387(或其他)上之后,我想显示一条消息。但是我不能在这个类中这样做,它需要在主类中完成。
但不知何故,我无法让它发生。
如果counter== 387 我的程序是否有可能返回MainActivity()并调用函数?

public class MainActivity extends Activity {
}

我知道如何制作吐司等,但它不适用于解压缩类。
当计数器为某个数字时,欢迎任何解决方案显示消息。

我希望你们知道我的意思,否则说出来,总是可以提供更多信息。

试过这个: Toast.makeText(getBaseContext(),"test", Toast.LENGTH_SHORT).show();
getBaseContext 未定义,因为解压缩类不是 Activity。

日志猫:

11-15 09:20:50.315: E/UnZip(6495): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-15 09:20:50.315: E/UnZip(6495):  at android.os.Handler.<init>(Handler.java:121)
11-15 09:20:50.315: E/UnZip(6495):  at android.widget.Toast$TN.<init>(Toast.java:317)
11-15 09:20:50.315: E/UnZip(6495):  at android.widget.Toast.<init>(Toast.java:91)
11-15 09:20:50.315: E/UnZip(6495):  at android.widget.Toast.makeText(Toast.java:233)
11-15 09:20:50.315: E/UnZip(6495):  at com.bigflow.dark_legacy.MainActivity$1.message(MainActivity.java:45)
11-15 09:20:50.315: E/UnZip(6495):  at com.bigflow.dark_legacy.Decompress$UnZipTask.unzipEntry(Decompress.java:109)
11-15 09:20:50.315: E/UnZip(6495):  at com.bigflow.dark_legacy.Decompress$UnZipTask.doInBackground(Decompress.java:70)
11-15 09:20:50.315: E/UnZip(6495):  at com.bigflow.dark_legacy.Decompress$UnZipTask.doInBackground(Decompress.java:1)
11-15 09:20:50.315: E/UnZip(6495):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-15 09:20:50.315: E/UnZip(6495):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-15 09:20:50.315: E/UnZip(6495):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-15 09:20:50.315: E/UnZip(6495):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-15 09:20:50.315: E/UnZip(6495):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-15 09:20:50.315: E/UnZip(6495):  at java.lang.Thread.run(Thread.java:856)
4

1 回答 1

2

更新:我的意思是,当您说您无法从 Decompress 发出 Toast 消息(有点懒得阅读所有内容)时,我是相信您的话,但是尝试将您的活动上下文作为参数发送,然后在中调用 Toast onPostUpdate.

在这种情况下,您最好的选择是在构造函数中也发送一个侦听器作为参数。因此,在您调用的 Activity 中的任何位置Decompress

主要活动

public class MainActivity extends Activity {
/*Stuff and more stuff*/
    new Decompress (String fileName, String filePath, String destinationPath, 
    new MyListener(){
        public void message() { 
            Toast.makeText(MainActivity.this, "Your message", Toast.LENGTH_SHORT).show()
        }
    });
}

解压类

public class Decompress extends Observable {

static final String TAG = "UnZip";
int counter;
private String mFileName, mFilePath, mDestinationPath;
MyListener listen;

public Decompress (String fileName, String filePath, String destinationPath, MyListener listener) {
    mFileName = "bigflow";
    mFilePath = Environment.getExternalStorageDirectory().toString();
    mDestinationPath = Environment.getExternalStorageDirectory() + "/bigflow/";
    listen = listener;
}
//......

当然,在某处定义的实际接口

public interface MyListener{
    void message();
}

哦,差点忘了

finally {
         outputStream.close();
         inputStream.close();
         counter++;
         if(counter == 387) listen.message();
         }

不确定这是否是您想要的方式,或者在该部分中,但您明白了。

干杯!

于 2012-11-15T07:40:20.817 回答