使用Kotlin的Android AsyncTask

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:

异步任务的三个类型参数是:

  1. Params: The type of the parameters sent to the AsyncTask.

    Params :发送到AsyncTask的参数的类型。
  2. Progress: The type of the progress units published during the background computation.

    Progress :在后台计算期间发布的进度单位的类型。
  3. 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具有四种方法,这些方法在异步任务执行的生命周期中的不同时间触发。

  1. 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相关的任务。
  2. doInBackground: The background execution code goes i
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值