2016-06-23 2 views
3

알림 영역에서 현재 다운로드 파일을 취소하고 싶습니다. &을 추가하고 싶습니다. 알림 다운로드 진행률 하단에 버튼을 취소하고 싶습니다. 버튼을 클릭하십시오. 해당 취소 버튼을 클릭하면 다운로드가 취소됩니다. 여기 내 수업 다운로드 다운로드 파일을 수행하는 데 사용. 내가해야 할 수정 사항은 무엇입니까?다운로드 파일 실행을 취소하려면 어떻게해야합니까?

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

답변

2

시간의 대부분은 당신이 작업이 취소되어 있는지 검사를 추가 할 수 있습니다

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

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

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

루프에 소요되는,

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

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

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

를 호출하여 다운로드를 취소
yourAsyncTask.cancel() 
+0

하지만 어떻게 추가 할 수 있습니까? 알림에 "취소 버튼"이 있습니까? –

+0

http://stackoverflow.com/a/16195981/493321 여기에 대한 답변은 – basilisk

+0

입니다. 여기에는 3 가지 유형의 알림이 모두 나와 있습니다. http://www.androidwarriors.com/2016/05/android-notification-example-android.html –

0

취소 할 수 있습니다. ctask 강제

는 확인이

는 활동에 AsyncTask를 선언 :

private YourAsyncTask mTask; 

인스턴스화를 다음과 같이 :

mTask = new YourAsyncTask().execute(); 

의 킬/이런 식으로 취소

mTask.cancel(true); 
관련 문제