下载完成后,Android异步任务不会停止

时间:2014-03-29 09:26:38

标签: android google-maps-android-api-2 android-download-manager

我正在尝试使用Async Task中的DownloadManager下载文件。这是doInBackground()方法

protected Boolean doInBackground(String... params) {
        DownloadManager mgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        //String DIRECTORY_DOWNLOADS = m_context.getExternalFilesDir(null).getAbsolutePath();

        Uri uri = Uri.parse(params[0]);
        typ_mapy = Integer.parseInt(params[2]);
        File download = new File(getExternalFilesDir(null) ,params[1]);
        if(download.exists()){
            info = "Existuje";
            nazev_souboru=params[1];
            return false;
        }else{

            try {
                long lastDownload = mgr.enqueue(new DownloadManager.Request(uri)
                                    .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI)
                                    .setAllowedOverRoaming(false)
                                    .setNotificationVisibility(Request.VISIBILITY_HIDDEN)
                                    .setDestinationInExternalFilesDir(m_context,"/",params[1]));
                    Cursor c = mgr.query(new DownloadManager.Query().setFilterById(lastDownload));
                    while(c.moveToFirst())
                    {


                        publishProgress(String.valueOf(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))), String.valueOf(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))));
                        /*if(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))>=c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))){
                            c.close();
                            return true;
                        }*/
                        c.close();
                        c = mgr.query(new DownloadManager.Query().setFilterById(lastDownload));
                        c.moveToFirst();
                    }

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

你可以看到我在那里发表评论。我认为这个条件将在下载完成时停止循环,但是当它在那里时,它只是立即调用onPostExecute()(虽然下载正在运行并完成)。

问题是它停止调用onProgressUpdate(),我更新了我的进度条。

有没有办法如何将它保留在while循环中以便进度条更新?如果它是这样的话,它会在无限循环中停留在那里。如果我取消注释,它会立即结束。

修改: 我通过改变循环解决了这个问题:

boolean downloading = true;
                    while (downloading) {

                        DownloadManager.Query q = new DownloadManager.Query();
                        q.setFilterById(lastDownload);

                        Cursor cursor = mgr.query(q);
                        cursor.moveToFirst();
                        long bytes_downloaded = cursor.getLong(cursor
                                .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                        long bytes_total = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                        publishProgress(String.valueOf(bytes_total), String.valueOf(bytes_downloaded));
                        if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                            downloading = false;
                        }
                    }

在此处找到答案:Show Download progress inside activity using DownloadManager

1 个答案:

答案 0 :(得分:2)