【问题标题】:Stop on going download on button click android停止继续下载按钮单击android
【发布时间】:2013-11-12 09:57:50
【问题描述】:

我正在开发一个应用程序,主要专注于从 Web 服务下载文件并将其存储在 sd 卡中。一切都很顺利,直到我的客户需要取消正在进行的下载。我尝试了很多方法,但我失败了。所以任何人都可以帮助我并发布一些sn-ps以取消下载。 Intent 服务将更受青睐。

编辑:

                    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 上下载了部分 在此先感谢:)

【问题讨论】:

  • 你是在下载AsyncTask中的文件吗?
  • @Apoorv 不。我正在使用 IntentService。

标签: android service download intentservice


【解决方案1】:

您需要从您的服务中检查它是否已停止,并且您在该行完成了一半

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;

【讨论】:

  • 感谢您的回复。我在 onDestroy 方法中设置 stop - true 。因为一旦我们自动调用 stopservice,onDestroy 就会被调用。 public void onDestroy() { Log.i("Inside Intent Service onDestroy->", "true");字符串通知;停止=真; }
  • 不行吗?如果不将您的 onClick 实现发布为编辑
  • public void onClick(DialogInterface dialog,int id) { Log.i("服务停止前的Aftr---->>>>>", "true"); Log.i("Intent obj","Intent 检查..."+intent); if(intent!=null){ Common.isDownloadProgress = false;停止服务(意图);
  • 我已经添加了..请检查
【解决方案2】:

我认为你应该使用 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();
}

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

【讨论】:

    【解决方案3】:

    在您的按钮点击时添加此项。修改 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      • 2015-04-25
      相关资源
      最近更新 更多