安卓:可以AsyncTask的回报在另一个线程比UI线程?线程、AsyncTask、UI

2023-09-04 12:24:41 作者:痴貨先森

Android的文档说的AsyncTask postExecute()被称为UI线程。我下的是IM pression的 postExecute()从线程调用其中执行()被称为:我一直在使用在自己的线程后台服务的的AsyncTask,并postExecute()被调用的服务线程,而不是主线程。 不过,我最近有一个问题,postExecute()不被调用,同时引发了异常:发送消息到一个处理一个死线。

Android documentation says that AsyncTask postExecute() is called on the UI thread. I was under the impression that postExecute() was called from the Thread where execute() was called : I have been using an AsyncTask in a background Service with its own thread, and postExecute() was called in the service thread, not the main thread. However, I recently had an issue with the postExecute() not being called at all, while an exception was thrown : " sending a message to a Handler on a dead thread".

它是如何精确地说: - 应AsyncTask的只能用于主线程? - 如果没有,在哪个线程 postExecute()应该被称为:总是在UI线程,或执行()调用线程?

How is it exactly : - shall AsyncTask be used ONLY from the main thread ? - if not, in which thread postExecute() is supposed to be called : always the UI thread, or the execute() calling thread ?

感谢您

推荐答案

的AsyncTask的pre和postExecute方法创建任务实例的线程上调用。当你调用execute()没有关系。在其上创建任务的线程必须是环线,并在实践中应始终是主线程(或UI线程。)

AsyncTask's pre and postExecute methods are invoked on the thread on which the task instance was created. Where you call execute() doesn't matter. The thread on which you create the task must be a looper thread and in practice should always be the main thread (or UI thread.)