AsyncTask in Android with Kotlin

前端 未结 10 1514
离开以前
离开以前 2020-12-13 12:31

How to make an API call in Android with Kotlin?

I have heard of Anko . But I want to use methods provided by Kotlin like in Android we have Asynctask for background

10条回答
  •  -上瘾入骨i
    2020-12-13 13:04

    You can get a similar syntax to Anko's fairly easy. If you just wan't the background task you can do something like

    class doAsync(val handler: () -> Unit) : AsyncTask() {
        override fun doInBackground(vararg params: Void?): Void? {
            handler()
            return null
        }
    }
    

    And use it like

    doAsync {
        yourTask()
    }.execute()
    

提交回复
热议问题