2016-06-23 18 views
3

Ich möchte die aktuelle Download-Datei aus dem Infobereich & abbrechen Ich möchte hinzufügen Abbrechen Schaltfläche am unteren Rand der Benachrichtigung Download-Fortschritt. Mit einem Klick auf diesen Abbrechen-Button sollte der Download abgebrochen werden. Hier ist meine Klasse DownloadSong mit denen ich Download-Datei durchführen. Welche Änderungen muss ich tun?Wie kann ich laufende Download-Dateien abbrechen?

public class DownloadSong extends AsyncTask<String, Integer, String> { 

Activity activity; 
public static String songName, songURL; 

private NotificationHelper mNotificationHelper; 
public static int notificationID = 1; 
boolean download = false; 
NotificationManager nm; 

public DownloadSong(Activity activity, String songName, String songURL) { 
    this.activity = activity; 
    this.songName = songName; 
    this.songURL = songURL; 

    mNotificationHelper = new NotificationHelper(activity, songName); 
} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 

    notificationID++; 
    mNotificationHelper.createNotification(notificationID); 
} 

@Override 
protected String doInBackground(String... file_URL) { 

    try { 
     URL url = new URL(songURL); 
     HttpURLConnection URLconnection = (HttpURLConnection) url.openConnection(); 
     URLconnection.setRequestMethod("GET"); 
     URLconnection.setDoOutput(true); 
     URLconnection.connect(); 

     // Detect the file length 
     int fileLength = URLconnection.getContentLength(); 

     File fSDcard = Environment.getExternalStorageDirectory(); 
     String strSdcardPath = fSDcard.getAbsolutePath(); 

     File fDirectory = new File(strSdcardPath + "/GSD"); 

     if (!fDirectory.exists()) { 
      fDirectory.mkdir(); 
     } 

     File fMyFile = new File(fDirectory.getAbsolutePath() + "/" + songName + ".mp3"); 
     Log.e("Download file name ", fMyFile.toString()); 

     FileOutputStream out = new FileOutputStream(fMyFile, true); 

     InputStream input_File = URLconnection.getInputStream(); 

     byte[] data = new byte[1024]; 
     int total = 0; 
     int count; 

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

      total += count; 
      publishProgress((int) (total * 100/fileLength)); 

      out.write(data, 0, count); 
     } 

     out.flush(); 
     out.close(); 
     input_File.close(); 

    } catch (IOException e) { 

     Log.e("Download Error : ", "Failed"); 

    } 
    Log.e("Download status", " Complete "); 

    return null; 
} 

@Override 
protected void onPostExecute(String s) { 
    Toast.makeText(activity, "Song " + "'" + songName + "'" + " downloaded successfully", Toast.LENGTH_LONG).show(); 
    //pDialog.dismiss(); 

    if (download) { 
     Toast.makeText(activity, "Could Not Connect to Server.", Toast.LENGTH_LONG).show(); 
     mNotificationHelper.clearNotification(); 
    } else 
     mNotificationHelper.completed(); 
} 

@Override 
protected void onProgressUpdate(Integer... progress) { 
    //pDialog.setProgress(progress[0]); 

    mNotificationHelper.progressUpdate(progress[0]); 

    super.onProgressUpdate(progress); 
} 
} 

Antwort

2

Die meiste Zeit in der Schleife ausgegeben wird

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

    total += count; 
    publishProgress((int) (total * 100/fileLength)); 

    out.write(data, 0, count); 
} 

Sie einen Scheck hinzufügen können, um zu sehen, ob die Aufgabe abgebrochen wird,

while ((count = input_File.read(data)) != -1 && !isCancelled()) { 

    total += count; 
    publishProgress((int) (total * 100/fileLength)); 

    out.write(data, 0, count); 
} 

und brechen Sie den Download von

Aufruf
yourAsyncTask.cancel() 
+0

Aber wie kann ich hinzufügen "Abbrechen" Button zur Benachrichtigung? –

+0

http://Stackoverflow.com/a/16195981/493321 hat eine gute Antwort für diese – basilisk

+0

Lesen Sie hier alle 3 Arten von Benachrichtigungen. http://www.androidwarriors.com/2016/05/android-notification-example-android.html –

0

Sie können asyn abbrechen ctask gewaltsam

prüfen Dieses

Ihre AsyncTask in Ihrer Tätigkeit erklären:

private YourAsyncTask mTask; 

instantiate es wie folgt aus:

mTask = new YourAsyncTask().execute(); 

Kill/cancel es wie folgt aus:

mTask.cancel(true);