问题描述
我只是发现背后是什么在不使用上下文doInBackground事实()
。在事实上,我们不能更新内部doInBackground(用户界面)直接更新UI内部doInBackground()我们通过来调用其他线程publishProgress()
方法,该方法负责调用 onProgressUpdate()
。结果
因此,在短期我只是想知道什么是 UI线程
和的AsyncTask
,为什么AsyncTask的UI中执行之间的化学反应主题。
I am just finding what is the fact behind not using Context inside doInBackground()
. In-fact we can't update the UI inside doInBackground() directly, to update the UI inside doInBackground() we have to call other thread via publishProgress()
method which is responsible to call onProgressUpdate()
.
So in short I just wanted to know what is the chemistry between UI thread
and AsyncTask
, and why AsyncTask execute in UI Thread.
推荐答案
问:为什么语境不能的AsyncTask的doInBackground()使用?
背景顾名思义,应用程序/对象的当前状态的其范围内。它让新创建的对象理解什么已经持续。通常情况下你怎么称呼它,获得有关你的程序(活动,包/应用程序)的另一部分信息。
Context as the name suggests, its the context of current state of the application/object. It lets newly created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity, package/application)
在哪里为doInBackground()有没有关系/(上下文)或不相关的present活动时,其在 doInBackground()
。你必须在主线程的访问权限。它完全是新的活动,即使你的主线程/活动已停止工作,这将完成其任务。
Where as doInBackground() has no relation/(context) or is not related to the present Activity when its in doInBackground()
. You have no access to the main thread. Its completely a new activity which will perform its task even if your Main Thread/Activity has stopped working.
的AsyncTask没有主线程或您用来调用运行it.It一个单独的线程来执行给它的任务主UI线程上运行
AsyncTask do not run on the main thread or the main UI thread which you used to call it.It runs on a separate thread to perform the task given to it
要能够做更改UI的AsycTask完成后,你必须调用
To be able to do changes to the UI after the AsycTask completes, you have to call
protected void onPostExecute(String string)
{
Toast c=Toast.makeText(getApplicationContext(), string, Toast.LENGTH_LONG);
c.show();
}
所有UI的变化可以做的保护无效onPostExecute()
在短期 doInBackground()
不能做你的主线程的任何变化,而不是在你的主UI /线程依赖所以在没有使用上下文的问题 doInBackground()
In short doInBackground()
Cannot do any changes to your Main Thread and Is not Dependent on your Main UI/Thread so there is no question of using context in doInBackground()
问:为什么我们能够在preExecute()和onPostExecute使用上下文()?
一个AsycTask有
A AsycTask has
1在preExecute。()
pre /创建一个新的线程之前(可以做更改主线程)
1.onPreExecute()
Pre/Before creating a new Thread (Can do changes to Main Thread )
在preExecute()有背景的主线程/ AsycTask仍然在主线程
onPreExecute() has context to the main thread / AsycTask Still in Main thread
2 doInBackground()
已经建立了自己的一个新的线程来执行特定的任务。一旦在这个国家/线程你不能做任何事情,直到它完成。
2.doInBackground()
Has created a New thread of its own to perform the task given. Once in this State/Thread You cannot do anything until its complete.
doInBackground()否语境。在其own.Once新的线程被创建,将不考虑完成主线程其特定的任务创建新的线程被杀/停止。/ AsycTask在新线程。
doInBackground() No Context . New thread is created of its own.Once new thread is created it will complete its given task irrespective of the main thread getting Killed/Stopped./ AsycTask in new thread.
3 onPostExecute()
- 后doInBackground()完成其任务onPostExecute()被调用这样计算的结果可以通过主线程使用
3.onPostExecute()
- After doInBackground() completes its task onPostExecute() is called So that results of the computation can be Used by the main thread
onPostExecute()有背景的主线程/ AsycTask回主线程。
onPostExecute() has context to the main thread /AsycTask back in Main Thread.
这篇关于为什么语境不能的AsyncTask的doInBackground()使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!