我遇到了这个问题,我挣扎了几个小时,我认为这需要几分钟。
我尝试了很多事情,比如使用线程等,但我认为这可以更容易地完成。
这是我的解压缩类(解压缩器):
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 未定义,因为解压缩类不是活动。
日志猫:
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)
更新:我的意思是,当你说你不能从 Decompression 发出 Toast 消息时,我是在相信你的话(有点懒得阅读所有内容),但尝试在你的活动上下文中作为参数发送,然后在 onPostUpdate
中调用 Toast。
在这种情况下,最好的选择是将侦听器作为构造函数中的参数发送。因此,无论您在活动中调用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();
}
不太确定这是否是您想要的,或者在那个部分,但你明白了。
干杯!