2017-02-12 2 views
0

Ich versuche, die Datei mit AsyncTask herunterzuladen und wollte auch den Abbrechen-Button im Fortschrittsdialog implementieren, um den Download abzubrechen.Async-Task wird nicht abgebrochen

Ich denke, das Problem ist in "doInBackground" -Methode. hier ist mein AsyncTask:

public class Download_result extends AsyncTask<String,Integer,Void>{ 
ProgressDialog progressDialog; 
Context context; 
String pdfFile; 


Download_result(Context context, String pdfFile){ 
this.context=context; 
this.pdfFile=pdfFile; 
} 
@Override 
protected void onPreExecute() { 
    progressDialog = new ProgressDialog(context); 
    progressDialog.setTitle("Downloading..."); 
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
    progressDialog.setMax(200); 
    progressDialog.setCancelable(false); 
    progressDialog.setProgress(0); 
    progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      Download_result.this.cancel(true); //cancel asynctask here 
      dialog.dismiss(); 
     } 
    }); 
    progressDialog.show(); 
} 

@Override 
protected Void doInBackground(String... params) { 
    //given below 
} 
@Override 
protected void onProgressUpdate(Integer... values) { 
    progressDialog.setProgress(values[0]);  
} 

@Override 
protected void onPostExecute(Void result) { 
    progressDialog.cancel();   

} 
} 

"doInBackground" Methode:

@Override 
protected Void doInBackground(String... params) {  

     String url_1=params[0]; 
     int file_length=0; 
     try { 
      URL url = new URL(url_1); 
      URLConnection urlConnection = url.openConnection(); 
      urlConnection.connect(); 
      file_length=urlConnection.getContentLength(); 
      filesize=file_length; 
      File sdCard = Environment.getExternalStorageDirectory(); 
      File new_folder = new File (sdCard.getAbsolutePath() + "/xxx"); 

      File input_file = new File(new_folder,pdfFile); 
      InputStream inputStream = new BufferedInputStream(url.openStream(),8192); 
      byte[] data=new byte[1024]; 
      int total=0,count=0; 
      OutputStream outputStream = new FileOutputStream(input_file); 
      while ((count=inputStream.read(data))!=-1){ 
       total+=count; 
       outputStream.write(data,0,count); 

       int progress= (total*200)/file_length; 
       downloadedsize=total; 

       publishProgress(progress); 
       if(isCancelled()){ 
        break; or return null; // same result 
       } 


      } 
      inputStream.close(); 
      outputStream.close(); 


     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     }   
    return null; 
} 

enter image description here

Antwort

1

try this:

boolean downloadstatus = true; 

@Override 
protected void onPreExecute() { 
     progressDialog = new ProgressDialog(context); 
     progressDialog.setTitle("Downloading..."); 
     progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     progressDialog.setMax(200); 
     progressDialog.setCancelable(false); 
     progressDialog.setProgress(0); 
     progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      download.cancel(true); 
      downloadstatus=false; //add boolean check 
      dialog.dismiss(); 
     } 
     }); 
     progressDialog.show(); 
} 

Jetzt in Ihrem doInbackGround()

while ((count=inputStream.read(data))!=-1){ 

      if(!your_AsyncTask.isCancelled() || downloadstatus !=false){ 
       total+=count; 
       outputStream.write(data,0,count); 
       int progress= (total*200)/file_length; 
       downloadedsize=total; 

       publishProgress(progress); 
      }else{ 
       break; 
      } 
     } 
+0

tatsächlich meine asynctask bekommen abbrechen, siehe meine Antwort auf meine Post. Anyway tanx für die Antwort – wangz

+0

das macht Sinn ... der Download wird gestoppt – rafsanahmad007

0

Dies ist eine Antwort auf meine eigenen Beitrag (in Zukunft brauchen jemanden, dies auch sein mag):

Actualy bekommen meine AsyncTask abbrechen, aber ich weiß nicht, weil ich dachte, dass, wenn wir die AsyncTask die Datei abbrechen sollte nicht da sein. Aber tatsächlich, wenn wir Async-Task abbrechen, wird die Datei gespeichert, aber mit der kleineren Größe.

z. Angenommen, die Datei ist 1 MB und ich habe asynctask abgebrochen, während der Fortschrittsdialog 50% anzeigt, dann wird nur die 500kb Datei gespeichert, und ich dachte, dies ist die eigentliche Datei. Entschuldige meinen Fehler. Also die Logik ist, wenn jemand drücken Abbrechen müssen Sie die Hälfte heruntergeladene Datei zu löschen. und Entschuldigung für mein Englisch, es ist schrecklich ich weiß.

Verwandte Themen