安卓的AsyncTask块返回键调度事件事件、AsyncTask

2023-09-07 02:42:14 作者:时间冲淡了誓言

现在的问题是:

我的活动开始于ONSTART的AsyncTask的()。 在doInBackground方法我做一个简短的WebRequest,并根据您的网络引黄联接,这可能需要一点点,所以我想这个任务取消...

但..

在搜索,测试和调试现在我注意到,当用户presses在doInBackground方法的返回按钮,该KeyEvent IST总是出动后,我的doInBackground方法完成的时间。

所以我不必奇怪为什么AsyncTask的时候,用户presses的后退按钮永远不会被取消, 在AsyncTask.cancel(TRUE)被调用为时已晚......(即使我不知道,如果取消(真)将帮助)

所以是AsyncTask的和后退按钮这正常behavoiur?

这不能是正常的,因为如何在用户曾经得到从慢速连接的活动回来?

等待超时?

我乞求帮助,取消异步WebRequest的应该是可能的:)

解决方案   

当用户presses在doInBackground方法的返回按钮,该KeyEvent总是出动后,我的doInBackground方法完成。

好友问问

没有,这是不正确的。

如果当一个AsyncTask的是我的主要活动(从我的服务器下载文件)运行的,我打我的设备上的后退按钮,该活动将立即关闭。

什么是真实的,但是,是的AsyncTask将继续运行任何code是在其 doInBackground()方法,除非我明确取消(但你有种中已经知道了)。

据我所知,你的'的WebRequest(无论那是)阻止你的 doInBackground()方法,正因为如此,任何企图取消在的onPause()的onStop 的onDestroy()等不会有任何效果。

由于advantej指出, AsyncTask.cancel(...)方法的工作方式是,它会导致 isCancelled 设置为真。为了成功地取消的AsyncTask的 doInBackground()方法,你需要定期检查 isCancelled 。示例...

  @覆盖
保护无效doInBackground(字符串... PARAMS){

    而(!isCancelled){
        做一点事();
    }
}
 

现在的问题是,如果 DoSomething的()(例如你的'的WebRequest')挡住了,而循环再 isCancelled 将不检查,直到它完成。

The Problem is:

My Activity starts an AsyncTask in onStart(). In the doInBackground Method I make a short webrequest, and depending on your network connetion, this may take a little, so I want this task cancelable...

BUT..

After hours of searching, testing and debugging I noticed now, when the user presses the back button during the doInBackground Method, the Keyevent ist always dispatched AFTER my doInBackground method is finished.

So I dont have to wonder why the asynctask never is cancelled when the users presses the backbutton, the AsyncTask.cancel(true) is invoked too late..... (even if I am not sure if cancel(true) will help)

So is this normal behavoiur with asynctask and backbutton?

This cant be normal, because how should the user ever get back from the activity on slow connection?

wait for timeout?

I am Begging for Help, cancel an async webrequest SHOULD be possible :)

解决方案

when the user presses the back button during the doInBackground Method, the Keyevent is always dispatched AFTER my doInBackground method is finished.

No, this isn't true.

If I hit the BACK button on my device when an AsyncTask is running in my main Activity (downloading files from my server), the Activity is immediately closed.

What IS true, however, is the AsyncTask will continue to run whatever code is in its doInBackground() method UNLESS I explicitly cancel it (but you kind of know that already).

As far as I can tell, your 'webrequest' (whatever that may be) is blocking your doInBackground() method and because of that, any attempt to cancel it in onPause(), onStop, onDestroy() etc will never work.

As advantej points out, the way the AsyncTask.cancel(...) method works is that it causes isCancelled to be set to 'true'. In order to successfully cancel the AsyncTask's doInBackground() method you need to periodically check isCancelled. Example...

@Override
protected Void doInBackground(String... params) {

    while (!isCancelled) {
        DoSomething();
    }
}

The problem is if DoSomething() (for example your 'webrequest') is blocking the while loop then isCancelled won't be checked until it completes.