How to completely kill/remove/delete/stop an AsyncTask

前端 未结 7 2083
终归单人心
终归单人心 2020-11-28 09:57

I made an app that downloads videos from our server. The issue is:

When i cancel the downloading i call:

myAsyncTask.cancel(true)

I

相关标签:
7条回答
  • 2020-11-28 10:37

    You can use this code. I am downloading a OTA file as follow:

    static class FirmwareDownload extends AsyncTask<String, String, String> {
    
            public String TAG = "Super LOG";
            public String file;
            int lenghtOfFile;
            long total;
    
            @Override
            protected String doInBackground(String... f_url) {
                try {
                    int count;
                    Utilies.getInternet();
    
                    URL url = new URL(f_url[0]);
                    URLConnection connection = url.openConnection();
                    connection.connect();
                    lenghtOfFile = connection.getContentLength();
                    mProgressBar.setMax(lenghtOfFile);
                    InputStream input = new BufferedInputStream(url.openStream(), 8192);
                    String fileName = f_url[0].substring(f_url[0].lastIndexOf("/"), f_url[0].length());
                    File root = Environment.getExternalStorageDirectory();
                    File dir = new File(root.getAbsolutePath() + fileName);
    
                    Log.d(TAG, "trying to download in : " + dir);
                    dir.getAbsolutePath();
                    OutputStream output = new FileOutputStream(dir);
                    byte data[] = new byte[1024];
    
    
                    while ((count = input.read(data)) != -1) {
    
                        if (isCancelled())
                            break;
    
                        total += count;
                        mProgressBar.setProgress(Integer.parseInt("" + total));
                        Log.d("Downloading " + fileName + " : ", " " + (int) ((total * 100) / lenghtOfFile));
                        mPercentage.post(new Runnable() {
                            @Override
                            public void run() {
                                mPercentage.setText(total / (1024 * 1024) + " Mb / " + lenghtOfFile / (1024 * 1024) + " Mb");
                            }
                        });
                        output.write(data, 0, count);
                    }
    
                    output.flush();
                    output.close();
                    input.close();
    
                    //new InstallHelper().commandLine("mkdir data/data/ota");
    
                    File fDest = new File("/data/data/ota/" + fileName);
                    copyFile(dir, fDest);
                    FirmwareInstaller fw = new FirmwareInstaller();
                    fw.updateFirmware();
    
                } catch (Exception a) {
                    System.out.println("Error trying donwloading firmware " + a);
                    new InstallHelper().commandLine("rm -r  data/data/ota");
                    dialog.dismiss();
    
                }
                return null;
            }
    
        }
    

    So, If you want to cancel, just use this code:

     fDownload.cancel(true);
    
    0 讨论(0)
提交回复
热议问题