AsyncTask in Android with Kotlin

前端 未结 10 1516
离开以前
离开以前 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条回答
  •  时光取名叫无心
    2020-12-13 12:55

    Here is an example that will also allow you to update any UI or progress displayed to the user.

    Async Class

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

    Simple Usage

    doAsync {
        // do work here ...
    
        myView.post({
            // update UI of myView ...
        })
    }
    

提交回复
热议问题