restarting a download in android
<p>I have an app that downloads a file from a server. The download is in the Asynctask. I have a progress bar that shows the download progress. If the file is taking too long to complete downloading I give Cancel request. This just dismisses the progress dialog and when the file is clicked again it does not restart the download. How do I handle this scenario. A lot of research has left me a lot confused...I thought it is as simple as 
On downloading Show Download progress -> OnCancel, kill the download (unfortunately I cant find a way to kill an Asynctask) -> On click of the file, restart the download.....But alas, I dont seem to get a way...Please help...</p>

<p>Here is the source of what I do...</p>

<pre><code> class DownloadFileAsync extends AsyncTask&lt;String, String, String&gt; {



            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            //  showDialog(DIALOG_DOWNLOAD_PROGRESS);//to test i commented
                mProgressDialog = new ProgressDialog(Shelf_books.this);
                mProgressDialog.setMessage("Downloading file..");
                mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                mProgressDialog.setCancelable(true);
                /*mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface dialog) {
                        // TODO Auto-generated method stub
                        mytask.cancel(true);
                    }
                });*/

                mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        mytask.cancel(true);
                    }
                });
                mProgressDialog.show();
            }

            protected void onProgressUpdate(String... progress) {
                 Log.d("kunal",progress[0]);
                 mProgressDialog.setProgress(Integer.parseInt(progress[0]));
            }

            @Override
            protected void onPostExecute(String unused) {
                dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            }

            @Override
            protected void onCancelled() {
                Log.d("kunal","cancel method");
                running = false;
            }

            @Override
            protected String doInBackground(String... arg1) {
                // TODO Auto-generated method stub
                int count;
                if(isCancelled()==false)//test
                {//test
                try{
                URL url = new URL(arg1[0]);
                URLConnection conexion = url.openConnection();
                conexion.connect();

                int lenghtOfFile = conexion.getContentLength();
                Log.d("kunal", "Lenght of file: " + lenghtOfFile);

                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream(file);

                byte data[] = new byte[1024];

                long total = 0;

                    while ((count = input.read(data)) != -1) {
                        total += count;
                        publishProgress(""+(int)((total*100)/lenghtOfFile));
                        output.write(data, 0, count);
                    }

                    output.flush();
                    output.close();
                    input.close();
                } catch (Exception e) {
                    Log.d("kunal","exception occured");
                }
                }//test
                return null;
            }

        }
     /*creating async class ends here*/
}
</code></pre>

<p>And below is the call for onExecute, this is called on click of a button...</p>

<pre><code>public  void DownloadFile(String fileURL, File directory) {

   /*testing my code here starts */
   try {
   URL u = new URL(fileURL);
    new DownloadFileAsync().execute(fileURL);
  } catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
</code></pre>
