在Android下载DownloadManager和睡眠线程期间的ProgressBar

时间:2012-10-02 12:13:27

标签: android asynchronous download progress-bar download-manager

我使用DownloadManager从URL下载xml文件。然后我使用一个线程等待2秒钟完成将文件保存到SD卡。

我希望有一个活动圈,如here所示。实现这一目标的最简单方法是什么?我是否需要实施AsyncTask

我下载并等待的代码:

               //Download XML file from URL 
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));
                request.setTitle("Download von "+Name+".xml");

                // in order for this if to run, you must use the android 3.2 to compile your app
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                }

                request.setDestinationInExternalPublicDir(FileSeperator+"XML"+FileSeperator, Name + FileExtension);

                // get download service and enqueue file
                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                manager.enqueue(request);  

                File file = new File(Environment.getExternalStorageDirectory()+ FileSeperator 
                        +"XML"+FileSeperator+ Name + FileExtension);

                System.out.println("File existiert "+file.exists());

                //insert delay after download to finish save progress before starting to parse the xml
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

更新 这是我实现的AsyncTask

private class DownloadFile extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground(String... sUrl) {
        try {
            //Download XML file from URL 
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));
        request.setTitle("Download von "+Name+".xml");

        // in order for this if to run, you must use the android 3.2 to compile your app
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }

        request.setDestinationInExternalPublicDir(FileSeperator+"XML"+FileSeperator, Name + FileExtension);

        // get download service and enqueue file
        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(request);  

        File file = new File(Environment.getExternalStorageDirectory()+ FileSeperator 
                +"XML"+FileSeperator+ Name + FileExtension);

        System.out.println("File existiert "+file.exists());

        //insert delay after download to finish save progress before starting to parse the xml
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        } catch (Exception e) {
        }
        return null;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog.show();
    }
    protected void onPostExecute() {
        super.onPreExecute();
        pDialog.dismiss();
    }
    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
    }
}

我称之为:

    // instantiate it within the onCreate method
        pDialog = new ProgressDialog(CreateProject.this);
        pDialog.setMessage("Lädt...");
        pDialog.setIndeterminate(true);

        // execute this when the downloader must be fired
        DownloadFile downloadFile = new DownloadFile();
        downloadFile.execute();

3 个答案:

答案 0 :(得分:2)

我想是的,你应该用AsynsTask类实现它,它清晰,快速和简单。您可以阅读有关AsyncTask here

的简短教程

答案 1 :(得分:1)

您可以使用asynctask onPreExecute()显示您的进度对话框,在doInBackground()和onPostExecture中删除对话框并显示结果。

答案 2 :(得分:0)

只需在子类onPostExecute中调用super.onPostExecute()而不是super.onPreExecute(),否则它将无法正常工作