Android:AsyncTask 可以在 UI 线程之外的另一个线程中返回吗? [英] Android : can AsyncTask return in another thread than UI thread?

查看:15
本文介绍了Android:AsyncTask 可以在 UI 线程之外的另一个线程中返回吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 文档说 AsyncTask postExecute() 在 UI 线程上被调用.我的印象是 postExecute() 是从 execute() 被调用的线程调用的:我一直在后台服务中使用 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 线程,还是 execute() 调用线程?

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() 并不重要.您在其上创建任务的线程必须是 Looper 线程,并且实际上应该始终是主线程(或 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.)

这篇关于Android:AsyncTask 可以在 UI 线程之外的另一个线程中返回吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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