gpt4 book ai didi

android - 如何取消正在运行的下载文件?

转载 作者:搜寻专家 更新时间:2023-11-01 09:46:09 24 4
gpt4 key购买 nike

我想从通知区域取消当前下载文件&我想在通知下载进度的底部添加取消按钮。通过单击该取消按钮,应该取消下载。这是我的类(class) DownloadSong,我使用它来执行下载文件。我需要做哪些修改?

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

最佳答案

大部分时间都在循环中度过

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()

关于android - 如何取消正在运行的下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37982677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com