本文介绍了为什么语境不能的AsyncTask的doInBackground()使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是发现什么是背后的事实,在不使用内部的上下文 doInBackground() 。在-事实上,我们不能更新doInBackground内的UI()直接进行更新内部doInBackground(用户界面),我们不得不调用其他线程 publishProgress()方法,它负责叫 onProgressUpdate()
因此,在短期我只是想知道什么是 UI线程的AsyncTask ,以及为什么AsyncTask的用户界面中执行之间的化学反应主题。

I am just finding What is the fact behind the Not using the context inside the doInBackground(). In-fact we can't update the UI inside the doInBackground() directly,for update the UI inside doInBackground() we have to call other thread publishProgress() method which 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

要能够在AsycTask完成后做更改UI,你必须调用

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();

          }

所有的用户界面的变化中可以做保护无效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

在preExecute 1。() 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()使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 22:00