下载过程中的进度条与下载管理器和睡眠线程在Android中



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

我想有一个活动圈,如图所示。实现这一点的最简单方法是什么?我需要实现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();
                }

更新这是我实现的异步任务

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();
我认为

是的,你应该用AsynsTask类来实现这一点,它清晰、快速和简单。你可以在这里阅读一个关于AsyncTask的简短教程

你可以很好地满足你的要求 asynctask onPreExecute() 显示你的进度对话框 在doInBackground()中执行你的过程,onPostExecture关闭对话框并显示你的结果。

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

相关内容

  • 没有找到相关文章

最新更新