如何断开此AsyncTask?

时间:2015-12-05 07:00:34

标签: android android-asynctask

如果url不存在,如何关闭此连接和Asynctask。请帮助我。

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new DownloadFile().execute("https://i.stack.imgur.com/w4kCo.jpg");
}

下载任务如下,我无法控制它停止,进度已开始并仍然显示网址是否无效。

class DownloadFile extends AsyncTask<String,Integer,Long> {
    ProgressDialog mProgressDialog = new ProgressDialog(MainActivity.this);// Change Mainactivity.this with your activity name. 
    String strFolderName;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog.setMessage("Downloading Image ...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMax(100);
        mProgressDialog.setCancelable(false);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressDialog.show();
    }
    @Override
    protected Long doInBackground(String... aurl) {
        int count;
        try {
            URL url = new URL((String) aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();
            String targetFileName="downloadedimage.jpg";//Change name and subname

            int lenghtOfFile = conexion.getContentLength();
            String PATH = Environment.getExternalStorageDirectory()+"/myImage/";
            File folder = new File(PATH);
            if(!folder.exists()){
                folder.mkdir();//If there is no folder it will be created.
            }
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(PATH+targetFileName);
            byte data[] = new byte[1024];
            long total = 0;
            while ((count = input.read(data)) != -1) {
                total += count;
                       publishProgress ((int)(total*100/lenghtOfFile));
                output.write(data, 0, count);
            }
            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {}
        return null;
    }
    protected void onProgressUpdate(Integer... progress) {
         mProgressDialog.setProgress(progress[0]);
         if(mProgressDialog.getProgress()==mProgressDialog.getMax()){
            mProgressDialog.dismiss();

            Toast.makeText(getApplicationContext(), "Download Completed !", Toast.LENGTH_LONG).show();

         }
    }
    protected void onPostExecute(String result) {
    }
}

正确添加了所有必需的权限。

2 个答案:

答案 0 :(得分:0)

您可以在cancel()上致电AsyncTask。之后对isCancelled()的任何调用都将返回true。这并不一定意味着您的doInBackground方法将停止执行。您必须在该方法中手动检查isCancelled()才能正常退出。虽然在您拨打cancel()后,onPostExecute()将不会被呼叫。然而,onCancelled()会被调用。

答案 1 :(得分:0)

在代码中更改此行

class DownloadFile extends AsyncTask<String,Integer,Boolean> {

protected Boolean doInBackground(String... aurl) {

现在,当你的网址doInBackground方法仅从return false

时,网址失败了

现在你可以在

上查看
@Override
    protected void onPostExecute(Boolean result) {
        if(!result){
            mProgressDialog.dismiss();
        }
        super.onPostExecute(aLong);
    }