无法让 doInBackground 在 AsyncTask 中工作
Can't get doInBackground to work in AsyncTask
我是 AsyncTask 的新手,如果我的问题很愚蠢,请提前致歉。长话短说,我有一个处理一些文件的方法,我想在后台 运行 它。我的 class 在下面,问题是该方法在直接调用时工作正常,但当我尝试通过 doInBackground 调用它时绝对没有任何反应。
这个有效:
AttachFilesFromFolder.attach(files);
而这不是:
new AttachFilesFromFolder().execute(files);
有问题的class:
public class AttachFilesFromFolder extends AsyncTask<File[], Void, Void> {
@Override
protected Void doInBackground(File[]... files) {
try {
attach(files[0]);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
public static void attach(File[] files) throws InterruptedException {
for (File file : files) {
log("For loop started.");
File targetLocation = new File(Environment.getDataDirectory() + "/data/org.p4.epo.android/" + file.getName());
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(targetLocation);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
buf = null;
in.close();
in = null;
out.close();
out = null;
log("Copied file " + targetLocation.toString() + " successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
SdkManager.sharedInstance().addAttachment(targetLocation.toString(), file.getName(), AttachMode.asNewPage, 1, false);
log("Attached " + file.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
});
log("Thread T1 started.");
t1.start();
t1.join();
log("Thread T2 started.");
t2.start();
t2.join();
}
}
}
使用executeOnExecutor解决。成功了。
我是 AsyncTask 的新手,如果我的问题很愚蠢,请提前致歉。长话短说,我有一个处理一些文件的方法,我想在后台 运行 它。我的 class 在下面,问题是该方法在直接调用时工作正常,但当我尝试通过 doInBackground 调用它时绝对没有任何反应。
这个有效:
AttachFilesFromFolder.attach(files);
而这不是:
new AttachFilesFromFolder().execute(files);
有问题的class:
public class AttachFilesFromFolder extends AsyncTask<File[], Void, Void> {
@Override
protected Void doInBackground(File[]... files) {
try {
attach(files[0]);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
public static void attach(File[] files) throws InterruptedException {
for (File file : files) {
log("For loop started.");
File targetLocation = new File(Environment.getDataDirectory() + "/data/org.p4.epo.android/" + file.getName());
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(targetLocation);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
buf = null;
in.close();
in = null;
out.close();
out = null;
log("Copied file " + targetLocation.toString() + " successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
SdkManager.sharedInstance().addAttachment(targetLocation.toString(), file.getName(), AttachMode.asNewPage, 1, false);
log("Attached " + file.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
});
log("Thread T1 started.");
t1.start();
t1.join();
log("Thread T2 started.");
t2.start();
t2.join();
}
}
}
使用executeOnExecutor解决。成功了。