I use the DownloadManager to download a xml file from a URL. Then I use a Thread to wait 2 seconds to complete saving the file to the sd card.

I would like to have a activity circle as shown here. What is the easiest way to realize this? Do I need to implement a AsyncTask?

My code to download and wait:

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

UPDATE Here is my implemented 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);
    }
}

And I call it like that:

    // 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();
有帮助吗?

解决方案

You can very well full fill your requirement with asynctask onPreExecute() show your progress dialog do your process in the doInBackground() and onPostExecture dismiss the dialog and show your result.

其他提示

I think yes, you should implement this with AsynsTask class, it's clear, fast and easy. You can read a short tutorial about AsyncTask here

Just call super.onPostExecute() instead of super.onPreExecute() in the subclass onPostExecute or it won't work well

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top