为什么AsyncTask的doInBackground()中不能使用Context [英] Why Context can't be used in doInBackground() of AsyncTask

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

问题描述

我只是发现没有在doInBackground()中使用Context背后的事实是什么.实际上我们不能直接更新 doInBackground() 内部的 UI,要更新 doInBackground() 内部的 UI,我们必须通过负责调用 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.

推荐答案

Q 为什么AsyncTask的doInBackground()中不能使用Context?

上下文顾名思义,它是应用程序/对象当前状态的上下文.它让新创建的对象了解发生了什么.通常,您调用它是为了获取有关程序另一部分(活动、包/应用程序)的信息

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() 中时,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 不运行在主线程或您用来调用它的主 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();

          }

所有 UI 更改都可以在 protected void onPostExecute()

All the UI changes can be done in protected void 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()

Q 为什么我们可以在 onPreExecute() 和 onPostExecute() 中使用上下文?

AsycTask 有

A AsycTask has

1.onPreExecute() Pre/Before 创建新线程(可以更改主线程)

1.onPreExecute() Pre/Before creating a new Thread (Can do changes to Main Thread )

onPreExecute() 有主线程的上下文/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() 无上下文.新线程是自己创建的.一旦新线程被创建,它就会完成给定的任务,而不管主线程是否被杀死/停止./新线程中的 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()中不能使用Context的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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