gpt4 book ai didi

android - 从其他类调用主函数

转载 作者:行者123 更新时间:2023-11-29 21:55:11 25 4
gpt4 key购买 nike

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

这是我的解压类(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 之类的东西,但它在解压缩类中不起作用。
当计数器达到一定数量时,欢迎任何解决方案显示消息。

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

试过这个: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)

最佳答案

更新:我的意思是,当你说你不能从 Decompress 发出 Toast 消息时(有点懒惰阅读所有内容),我相信你的话,而是尝试将你的 Activities 上下文作为参数发送,然后在 onPostUpdate.

在这种情况下,您最好的选择是在您的构造函数中也发送一个监听器作为参数。因此,无论在您调用 Decompress 的 Activity 中的什么地方:

主 Activity

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;
}
//......

当然还有在某处定义的实际接口(interface)

public interface MyListener{
void message();
}

差点忘了

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

不太确定这是否是您想要的,或者在那个部分,但您明白了。

干杯!

关于android - 从其他类调用主函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13393135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com