0

我正在开发一个应用程序,主要专注于从 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);

意图服务类:

            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 上下载了部分提前谢谢:)

4

3 回答 3

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

希望这会给你一些提示......:)

于 2013-11-12T10:12:18.497 回答
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;
于 2013-11-12T10:12:43.820 回答
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();
于 2013-11-12T10:33:41.877 回答