我正在开发一个应用程序,主要侧重于从Web服务下载文件并将其存储在SD卡中。一切都很简单,直到我的客户需要取消下载。我尝试了很多方法,但我失败了。因此,任何人都可以帮助我,并发布一些片段取消下载。意图服务将是首选。
编辑:
Toast.makeText(ThumbnailView.this, R.string.Download_start, Toast.LENGTH_SHORT).show();
pb.setVisibility(View.VISIBLE);
info_icon.setVisibility(View.INVISIBLE);
langBtn.setVisibility(View.INVISIBLE);
name.setVisibility(View.INVISIBLE );
author.setVisibility(View.INVISIBLE );
lastReading.setVisibility(View.INVISIBLE);
intent = new Intent(ThumbnailView.this,
DownloadAndExtractFiles.class);
Common.isDownloadProgress = true;
intent.putExtra("BEAN", bean);
intent.putExtra("FROM", "library");
intent.putExtra("receiverTag", mReceiver);
startService(intent);
IntentService类:
try {
File file = new File(path, /* BOOK_ID */filename + fileformat);
if (file.exists())
file.delete();
file.createNewFile();
output = new FileOutputStream(file);
finalpath = path + "/" + filename + fileformat;
Log.d("book UNEXTR path", finalpath);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
if (Common.downloadChkLogout) {
// Changed on saturday 9th nov
//if (Common.isDownloadProgress) {
if (stopped) {
break;
}
total += count;
Bundle resultData = new Bundle();
resultData.putInt("progress",
(int) (total * 100 / lenghtOfFile));
bean.setProgress((int) (total * 100 / lenghtOfFile));
rec.send(UPDATE_PROGRESS, resultData);
output.write(data, 0, count);
}
}
success = true;
//}
output.flush();
output.close();
input.close();
} catch (Exception ex) {
ex.printStackTrace();
mError = "Download Failed";
System.out.println("Net Disconnect;:" + mError);
Toast.makeText(getApplicationContext(), mError,
Toast.LENGTH_LONG).show();
}
编辑:
public void onClick(DialogInterface dialog,int id) {
Log.i("Aftr before service stopped---->>>>>", "true");
Log.i("Intent obj","Intent check..."+intent);
if(intent!=null)
{
Common.isDownloadProgress = false;
stopService(intent);
这是我的取消点击
我已经发布了启动服务的代码,并在onHandleIntent上下载了部分代码 在此先感谢:)
答案 0 :(得分:0)
我应该使用AsyncTask下载项目,它也会在取消事件中提供。
假设您正在使用HttpGet下载文件,您的任务将看起来像......
public class FileDownloadingTask extends AsyncTask<Void, Void, Void> {
private String url = null;
private String destPath = null;
private HttpGet httpGet = null;
// Constructor
public FileDownloadingTask(String url, String destPath) {
this.url = url;
this.destPath = destPath;
}
// Create a progress dialog in your onPreExecute
// Start downloading in your doInBackground and save to destPath in sd-card
// Proform any thing your want in response to download process in onPostCreate
// Finally...
@Override
protected void onCancelled() {
super.onCancelled();
// Aborting http request
if (httpget != null) {
httpget.abort();
}
}
// Then in your cancel button of progress dialog call this.onCancel();
}
希望这会给你一些提示...... :)
答案 1 :(得分:0)
您需要检查服务中是否已停止,并且您已完成该行的一半
if (stopped)
break;
现在使stopped
成为一个静态布尔值,并在按钮点击时将其设置为true,
修改强>
您已经检查了Common.isDowloadProgress
,但已对其进行了评论,我认为您需要按照以下方式打破循环
while ((count = input.read(data)) != -1)
{
if (Common.downloadChkLogout)
{
if (Common.isDownloadProgress)
{
if (stopped)
{ break; }
total += count;
Bundle resultData = new Bundle();
resultData.putInt("progress",
(int) (total * 100 / lenghtOfFile));
bean.setProgress((int) (total * 100 / lenghtOfFile));
rec.send(UPDATE_PROGRESS, resultData);
output.write(data, 0, count);
}
else
{ break; }
}
}
success = true;
答案 2 :(得分:0)
点击按钮添加此项。修改if条件以满足您的需求。我正在上传视频,当我点击删除时,会发生这种情况: -
if(processFileUploadTask !=null && processFileUploadTask.getStatus() != AsyncTask.Status.FINISHED){
Thread t = new Thread(new Runnable() {
public void run() {
if(httpost != null ){
httpost.abort();
}
}
});
t.start();