我遇到了这个问题,我挣扎了几个小时,我认为这需要几分钟 我尝试了许多东西,比如使用线程等,但我认为这可以更轻松。
这是我的解压缩类(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。
logcat的:
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)
答案 0 :(得分:2)
<强>更新强>: 你的意思是,当你说你无法从Decompress发出一个Toast消息(有点懒惰读取所有内容)时,我正在接受你的话,但是尝试在您的Activities上下文中作为参数发送,然后在{{中调用Toast 1}}。
在这种情况下,您最好的选择是在构造函数中将监听器作为参数发送。因此,您在活动中的任何地方都可以调用onPostUpdate
:
<强> MainActivity 强>
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();
}
不确定这是你想要的,或者在那部分,但是你明白了。
干杯!