简体   繁体   English

如何在Android中正确处理AsyncTask取消?

[英]How properly handle AsyncTask cancelling in Android?

I implemented AsyncTask in my app to properly download file from internet and put data in my local DB. 我在我的应用程序中实现了AsyncTask,以正确地从Internet下载文件并将数据放入本地DB。 File is downloading and everything works. 文件正在下载,一切正常。 I also check if there is internet connection, and if there is no any, i am returning 0 in my doInBackground method, what is causing my AsyncTask calling "onPostExecute" method, and everything goes as it should. 我还检查是否有Internet连接,如果没有,我在doInBackground方法中返回0,是什么导致我的AsyncTask调用“ onPostExecute”方法,并且一切正常。 But I saw that if internet connection is poor, for instance I am at the edge of my WiFi range, AsyncTask sometimes downloads file for minutes. 但是我看到,如果互联网连接状况不佳,例如我处于WiFi范围的边缘,AsyncTask有时会下载文件几分钟。 This is not what I want. 这不是我想要的。 So I implemented im my AsyncTask, in onPreExecute method Handler. 因此,我在onPreExecute方法处理程序中实现了我的AsyncTask。

isFreezed = new Handler();
isFreezed.postDelayed(new Runnable() {
    @Override
    public void run() {
        if (!isCancelled() && getStatus() != AsyncTask.Status.FINISHED){
            cancel(true);
            //Log.i("callingActivity", "Zbyt wolne połączenie, anulowanie.");
            onPostExecute((long) 0);
        }
    }
},8000);

I am cancelling my AsyncTask. 我要取消我的AsyncTask。 I saw that when I cancel it, onPostExecute method is not executed, so I call it manually. 我看到当我取消它时,不会执行onPostExecute方法,因此我手动调用它。 And that's what is my problem, google says that it should not be called manually. 这就是我的问题,谷歌表示不应手动调用它。 Is my way of thinking good and putting handler to cancel task and go on with my app is a good practice, or should I do it other way? 我的思维方式是否很好,并且让处理程序取消任务并继续使用我的应用程序是一种好习惯,还是我应该采用其他方式呢? And am I right that when I will cancel AsyncTask, it's onPostExecute method won't be called? 我是对的,当我取消AsyncTask时,不会调用它的onPostExecute方法吗?

For that you can use Async task like 为此,您可以使用Async任务,例如

private class  AsyncTaskDemo extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Loading...");
        progressDialog.setCancelable(true);
        progressDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance

         // do stuff according to your need
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }

    }

    @Override
    protected void onCancelled() {

        super.onCancelled();
        progressDialog.dismiss();
        Toast toast = Toast.makeText(MainActivity.this,
                "Error connecting to Server", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 25, 400);
        toast.show();

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM