In this tutorial, we’ll learn and implement AsyncTask using Kotlin in our Android Application.
在本教程中,我们将在Android应用程序中使用Kotlin学习和实现AsyncTask。
什么是Android AsyncTask? (What is Android AsyncTask?)
Android AsyncTask is an abstract class that’s used to perform long operations in the background. We have to extend this class and implement the abstract methods to use async tasks in our app.
Android AsyncTask是一个抽象类,用于在后台执行较长的操作。 我们必须扩展此类并实现抽象方法以在我们的应用程序中使用异步任务。
inner class SomeTask extends AsyncTask<Params, Progress, Result>
The three type parameters of an asynchronous task are:
异步任务的三个类型参数是:
Params
: The type of the parameters sent to the AsyncTask.Params
:发送到AsyncTask的参数的类型。Progress
: The type of the progress units published during the background computation.Progress
:在后台计算期间发布的进度单位的类型。Result
: The type of the result of the background computation.Result
:后台计算Result
的类型。
AsyncTask方法 (AsyncTask Methods)
AsyncTask has four methods that are triggered at different times during the life cycle of async task execution.
AsyncTask具有四种方法,这些方法在异步任务执行的生命周期中的不同时间触发。
PreExecute
: This is invoked in UI thread before the AsyncTask is executed. We can show a ProgressBar or perform any UI related tasks in this method.PreExecute
:在执行AsyncTask之前在UI线程中调用它。 我们可以使用此方法显示一个ProgressBar或执行任何与UI相关的任务。doInBackground
: The background execution code goes i