Newer
Older
DevAndroid / app / src / main / java / keijumt / devandroid / common / Calculate.kt
package keijumt.devandroid.common

import android.os.AsyncTask

class Calculate(val callback: Callback<Int>) : AsyncTask<Int, Int, Int>() {

    // ui thread
    override fun onPreExecute() {
        println("onPreExecute")
    }

    override fun doInBackground(vararg params: Int?): Int {
        try {
            println("doInBackground")
            Thread.sleep(3000)
        } catch (e: Exception) {

        }
        val value = params.get(0) ?: 1
        return value * value
    }

    override fun onProgressUpdate(vararg values: Int?) {
        println("onPorgressUpdate")
    }

    override fun onPostExecute(result: Int?) {
        callback.execute(result)
    }

    interface Callback<T> {
        fun execute(value: T?)
    }
}