Async file downloader android
<p>I started creating file downloader class. Here is what i have:</p>

<pre><code>public class Downloader {

    //download url
    URL url;

    public Downloader(URL downloadURL){
        url = downloadURL;
    }


      public void toFile(File fName) {
            try {

                HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();

                urlConnection.setRequestMethod("GET");
                urlConnection.setDoOutput(true);

                urlConnection.connect();

                FileOutputStream fileOutput = new FileOutputStream(fName);

                InputStream inputStream = urlConnection.getInputStream();

                byte[] buffer = new byte[1024];
                int bufferLength = 0;

                while ((bufferLength = inputStream.read(buffer)) &gt; 0) {
                    fileOutput.write(buffer, 0, bufferLength);

                }

                fileOutput.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }




}
</code></pre>

<p>I need to call this downloader in new thread. Maybe someone could give me hint why i need to call new thread or asyc task for this.</p>

<pre><code>public void downloadFile(String urlToDownload,String path) throws MalformedURLException{




        new Thread(new Runnable() {
            public void run() {
                new Downloader(new URL(urlToDownload)).toFile(new File(path));

                 }


          }).start();
}
</code></pre>

<p>So when i want to downlaod my file it's strating downloading asynchronical.</p>

<p>I don't really want that.
 I want to create downloader where i could choose if i want  synchronously or async download a file.</p>

<p>But in the first place, i want to know when file was downloaded. How could i know this ?</p>

<p>Maybe someone had done this and could help me.
I need to know when file is downloaded.</p>

<p>Thanks.</p>
