【问题标题】:Can't get doInBackground to work in AsyncTask无法让 doInBackground 在 AsyncTask 中工作
【发布时间】:2020-08-15 02:15:23
【问题描述】:

我是 AsyncTask 的新手,所以如果我的问题很愚蠢,请提前道歉。长话短说,我有一个处理一些文件的方法,我想在后台运行它。我的课程在下面,问题是直接调用该方法时可以正常工作,但是当我尝试通过 doInBackground 调用它时绝对没有任何反应。

这有效: AttachFilesFromFolder.attach(files);

这不会: new AttachFilesFromFolder().execute(files);

有问题的班级:

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();
        }
    }
}

【问题讨论】:

  • 我看不出这不应该工作的任何原因,除非您的 AsyncTask 执行程序被阻止(由于此类问题,可能避免 AsyncTask 用于新代码)。您的 doInBackground 方法是否正在运行?向其中添加日志。
  • 此外,您当前对Threads 的使用完全没有意义。你是按顺序启动它们,然后立即加入它们,所以你没有得到任何好处,只是做了额外的工作。
  • 谢谢。我之前添加了一个日志,它证明 doInBackground 根本没有运行。至于我对线程的使用,目标是确保线程 1 在线程 2 启动之前完成。
  • 对,但既然你马上就要加入他们,为什么不直接在doInBackground 中完成所有这些工作呢?为什么要为线程烦恼?
  • 您确定会在 doInBackground 中一个接一个地运行吗?无论如何,看起来 onPreExecute 事件在我的班级中被正确触发,但 doInBackground 不是。我收到“尝试完成输入事件,但输入事件接收器已被释放”错误。

标签: java android android-asynctask


【解决方案1】:

通过使用 executeOnExecutor 解决。那行得通。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2012-11-05
    相关资源
    最近更新 更多