AsyncTask的onPostExecute不叫上2.3 [英] AsyncTask onPostExecute is not called on 2.3

查看:197
本文介绍了AsyncTask的onPostExecute不叫上2.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一块运行使用的AsyncTask后台code的。在code是类似这样的:

I have a piece of code running the background using AsyncTask. The code is similar to this:

private class MyAsyncTask extends AsyncTask<Void, Void, Object> {

    public MyAsyncTask() {
        // Some init. code
    }

    @Override
    protected Object doInBackground(Void... params) {
        // Doing some long running task.
        Log.v(TAG, "Finished everything. Returning ...");
        return result;
    }

    @Override
    protected void onCancelled(Object result) {
        // Either way, run the next one in the line.
        Log.v(TAG, "Task cancelled");
    }

    @Override
    protected void onPostExecute(Object result) {
        Log.v(TAG, "Task Completed.");
    }

}

在安卓4.0它工作得很好,但是当我在2.3上运行它(HTC野火是我唯一的测试手机现在),onPostExecute不叫。

On Android 4 it works just fine, but when I run it on 2.3 (HTC Wildfire is my only test phone for now), onPostExecute is not called.

我可以看到从 doInBackground 最后的消息,但没有从任 onCancelled 或<$ c中的消息$ C> onPostExecute 。有其他人面临同样的问题?我发现其中谈到了类似的问题这链接 ,但是没有给出明确的解决方案(只是建议使用正常的线程,而不是的AsyncTask )还是其precisely我所期待的。

I can see the last message from doInBackground, but none of the messages from either onCancelled or onPostExecute. has anyone else faced the same issue? I found this link which talks about a similar problem, but neither gives a clear solution (just suggests using normal thread instead of AsyncTask) nor its precisely what I am looking for.

有什么建议?

干杯

好吧,花了一天阅读code为的AsyncTask 处理程序和<后code>消息在这两个2.3和4,我有更多的线索,现在所发生的事情。

Ok, after spending a day and reading the code for AsyncTask, Handler, and Message in both 2.3 and 4, I have more clues now about what is happening.

一是多一点点关于我的设计。我有这无论是在应用程序以及在一些服务中使用一个单类从我的应用程序启动。因此,的AsyncTask code我发现上面既可以从活动(假设单通过活动访问),或从服务运行(如果单是通过服务访问)。

First a little bit more about my design. I have a singleton class which is used both in the app and also in some services started from my app. So the AsyncTask code I showed above can be run either from the activity (assuming the singleton is accessed via the activity), or from the service (if the singleton is accessed via service).

现在在API 11+,无论单是如何被访问时, onPostExecute 被称为就好了。在API 10及以下,如果单从活动的访问,则 onPostExecute 被称为就好了,但如果它通过服务访问, onPostExecute 不叫。

Now in API 11+, no matter how the singleton is accessed, the onPostExecute is called just fine. On API 10 and below, if the singleton is accessed from the activity, then onPostExecute is called just fine, but if it's accessed via service, onPostExecute is not called.

下面是关于它卡住更多的细节。 的AsyncTask 在2.3有一个 FutureTask提供将覆盖完成函数发送消息其内部处理程序如下:

Here is more details about where it get stuck. AsyncTask on 2.3 has an FutureTask which overrides done function to send a Message to its internal Handler as follow:

        mFuture = new FutureTask<Result>(mWorker) {
        @Override
        protected void done() {
            Message message;
            Result result = null;

            try {
                result = get();
            } catch (final InterruptedException e) {
                android.util.Log.w(AsyncTask.LOG_TAG, e);
            } catch (final ExecutionException e) {
                throw new RuntimeException("An error occured while executing doInBackground()",
                        e.getCause());
            } catch (final CancellationException e) {
                message = AsyncTask.sHandler.obtainMessage(AsyncTask.MESSAGE_POST_CANCEL,
                        new AsyncTaskResult<Result>(AsyncTask.this, (Result[]) null));
                message.sendToTarget();
                return;
            } catch (final Throwable t) {
                throw new RuntimeException("An error occured while executing "
                        + "doInBackground()", t);
            }

            message = AsyncTask.sHandler.obtainMessage(AsyncTask.MESSAGE_POST_RESULT, new AsyncTaskResult<Result>(AsyncTask.this, result));
            message.sendToTarget();
        }
    };

以下code调用 message.sendToTarget 这是应该调用的最后两行的handleMessage 的处理程序类,但它不会!现在我不知道为什么回调处理程序从活动中发生的,而不是从服务!我有一些解决方案如何解决这个(只需使用的AsyncTask类API15 +在任何时候我的code),但对这个原因的解释是非常AP preciated因为我有没有这方面的知识, !

The last two lines of the following code calls message.sendToTarget which is supposed to call handleMessage of the Handler class, but it does not! now I have no idea why the callback to the handler happens from an activity and not from a service! I have some solutions how to solve this (simply using AsyncTask class for API15+ in my code at all times), but any explanation about the cause of this would be very much appreciated as I have no knowledge on this!

感谢

推荐答案

正式文件很详细,尽量读它。

请检查此链接它可以帮助你。

Kindly check this link it might help you

http://foo.jasonhudgins.com/2010/05/限制-OF-asynctask.html

OK〜
注意:

OK~ notice:

onCancel功能,当你调用取消(boolean)方法执行。有你这样叫
功能?

onCancel function executes when you invoke cancel(boolean) method. have you called this function?

这篇关于AsyncTask的onPostExecute不叫上2.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆