【问题标题】:Android Async Task methods execution orderAndroid Async Task 方法执行顺序
【发布时间】:2015-09-12 18:38:44
【问题描述】:

我正在使用 AsyncTask 将一些文件下载到我的应用程序,当我刷新它们时,我希望可用文件列表自行更新。根据我在网上阅读的内容,下载部分将在“doInBackground”方法中完成,之后为了更新视图,我使用“onPostExecute”方法。当我这样做时,视图不会自行更新,我必须更改活动然后返回以更新它,我用来更新视图的方法有效,但似乎在所有文件已完成下载,因此它们不会与可用文件一起显示。

如果我错了,请纠正我,但是在 doInBackground 完全完成后是否调用了 onPostExecute ?如果没有,我该怎么做才能在文件下载完成之前不调用 onPostExecute ?

谢谢!

这是我正在使用的代码:

private class Documents_Task extends AsyncTask<String, String, Boolean> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Boolean doInBackground(String... params) {

            try {

                // Downloading the documents and savign them
                return true;

            } catch (Exception e) {
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean success) {

            if (success) {
                // Displaying the list of available files
            }
        }
    }

编辑:他是下载和保存文件的代码

String result = HttpManager.getData(params[0]);
                // Getting the names of the files I already have on my mobile
                File folder = (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/EHTProfs/Documents"));
                File[] listOfFiles = folder.listFiles();

                //String array, each element will correspond to a name of a file on my mobile
                String[] names = new String[listOfFiles.length];

                //Filling my array
                if (listOfFiles.length > 0)
                    for (int i = 0; i < listOfFiles.length; i++) {
                        if (listOfFiles[i].isFile())
                            names[i] = listOfFiles[i].getName();
                    }

                Context c = Documents.this;

                // Taking null out of the string result containing the names of the files on my server
                String sub_result = result.substring(1, result.length() - 6);

                // Comparing each file name on the server with those on my mobile,
                // if it exists do nothing, if not download it
                StringTokenizer st = new StringTokenizer(sub_result, "\",\"");
                Boolean exists;

                while (st.hasMoreTokens()) {
                    exists = false;
                    String fileName = st.nextToken();
                    if (names.length > 0)
                        for (int i = 0; i < names.length; i++) {
                            if (names[i].equals(fileName)) {
                                i = names.length;
                                exists = true;
                            }
                        }
                    if (!exists) {
                        downloadDocument(fileName);
                    }
                }

这是下载一个文件的方法

public void downloadDocument(String fileName) {

        DownloadManager mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        Uri downloadUri = Uri.parse("http://" + IP_ADDRESS_PORT + "/EhtprofsWEB/ehtprofs/document/" + fileName);
        DownloadManager.Request request = new DownloadManager.Request(
                downloadUri)
                .setAllowedOverRoaming(false)
                .setTitle(fileName)
                .setDestinationInExternalPublicDir(
                        Environment.DIRECTORY_DOWNLOADS + "/EHTProfs/Documents", fileName);
        mManager.enqueue(request);
    }

【问题讨论】:

  • 您可能需要详细说明您的下载/保存代码。
  • @headuck 我已经添加了代码

标签: android android-asynctask


【解决方案1】:

我认为您在实际下载完成之前传递了 doInBackground 的结果。您可以在 onSuccess of download 中写入 return。这将解决您的问题。

【讨论】:

  • 应该在哪里添加onSuccess?
【解决方案2】:

您在下载代码中调用的DownloadManager正在为您处理异步下载,下载请求立即返回。因此,您的doInBackground() 调用会在下载完成之前快速返回。

要实现你想要的,你可能根本不需要AsyncTask。相反,您可以设置广播接收器以在下载完成时收听 DownloadManager 广播。请参阅以下答案和链接的代码示例以供参考。

DownloadManager.ACTION_DOWNLOAD_COMPLETE broadcast receiver receiving same download id more than once with different download statuses in Android

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-03
    • 2021-03-07
    • 2022-08-14
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    相关资源
    最近更新 更多