停止继续下载按钮点击android
我正在开发一个应用程序,主要关注从web服务下载文件并将其存储在sd卡中。一切都很顺利,直到我的客户需要取消正在进行的下载。我尝试了很多方法,但都失败了。所以,有谁能帮我一下,并发布一些片段,取消正在进行的下载。意向服务将是首选 编辑:停止继续下载按钮点击android,android,service,download,intentservice,Android,Service,Download,Intentservice,我正在开发一个应用程序,主要关注从web服务下载文件并将其存储在sd卡中。一切都很顺利,直到我的客户需要取消正在进行的下载。我尝试了很多方法,但都失败了。所以,有谁能帮我一下,并发布一些片段,取消正在进行的下载。意向服务将是首选 编辑: Toast.makeText(ThumbnailView.this, R.string.Download_start, Toast.LENGTH_SHORT).show(); pb.
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();
}
编辑:
这是我的取消点击
我已经在OnHandleContent上发布了启动服务和下载部分的代码
提前感谢:)我想您应该使用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();
}
公共类文件下载任务扩展异步任务{
私有字符串url=null;
私有字符串destPath=null;
私有HttpGet HttpGet=null;
//建造师
公共文件下载任务(字符串url、字符串路径){
this.url=url;
this.destPath=destPath;
}
//在onPreExecute中创建进度对话框
//在您的doInBackground中开始下载并保存到sd卡中的destPath
//在onPostCreate中提供您想要的任何东西以响应下载过程
//最后。。。
@凌驾
受保护的void onCancelled(){
super.onCancelled();
//中止http请求
if(httpget!=null){
httpget.abort();
}
}
//然后在进程对话框的cancel按钮中调用这个.onCancel();
}
希望这会给你一些提示…:)你需要检查它是否在你的服务中被停止,你在这条线上已经完成了一半
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;
将此添加到您的按钮上,然后单击。修改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();
您是否在
AsyncTask
中下载该文件?@Apoorv Nope。我正在使用IntentService。谢谢您的回复。我正在onDestroy方法中设置stopped-true。因为一旦我们自动调用stopservice,onDestroy就会被调用。public void onDestroy(){Log.i(“内部意图服务onDestroy->”,“true”);字符串通知;stopped=true;}它不工作吗?如果未将onClick实现发布为editpublic void onClick(DialogInterface dialog,int id){Log.i(“服务停止前的Aftr-->>>>>>”,“true”);Log.i(“意图对象”,“意图检查…”+意图);如果(意图!=null){Common.isDownloadProgress=false;stopService(intent);我添加了..plz检查
if(processFileUploadTask !=null && processFileUploadTask.getStatus() != AsyncTask.Status.FINISHED){
Thread t = new Thread(new Runnable() {
public void run() {
if(httpost != null ){
httpost.abort();
}
}
});
t.start();