kotlin asynctask example
In this Article Today Learn kotlin asynctask example. Async Process also Background Process Like Download File or Other Process in Android App. Follow the kotlin asynctask example.
1.Asynct.kt:
package com.akash.kotlinlistview.AsyncTask
import android.app.Activity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import android.os.AsyncTask
import android.view.View
import android.widget.Toast
import com.akash.kotlinlistview.R
import kotlinx.android.synthetic.main.activity_asynct.*
import java.lang.ref.WeakReference
class Asynct : Activity() {
var myVariable = 10
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_asynct)
btnDoAsync.setOnClickListener {
val task = MyAsyncTask(this)
task.execute(10)
}
}
companion object {
class MyAsyncTask internal constructor(context: Asynct) : AsyncTask<Int, String, String?>() {
private var resp: String? = null
private val activityReference: WeakReference<Asynct> = WeakReference(context)
override fun onPreExecute() {
val activity = activityReference.get()
if (activity == null || activity.isFinishing) return
activity.progressBar.visibility = View.VISIBLE
}
override fun doInBackground(vararg params: Int?): String? {
publishProgress("Sleeping Started") // Calls onProgressUpdate()
try {
val time = params[0]?.times(1000)
time?.toLong()?.let { Thread.sleep(it / 2) }
publishProgress("Half Time") // Calls onProgressUpdate()
time?.toLong()?.let { Thread.sleep(it / 2) }
publishProgress("Sleeping Over") // Calls onProgressUpdate()
resp = "Android was sleeping for " + params[0] + " seconds"
} catch (e: InterruptedException) {
e.printStackTrace()
resp = e.message
} catch (e: Exception) {
e.printStackTrace()
resp = e.message
}
return resp
}
override fun onPostExecute(result: String?) {
// execution of result of Long time consuming operation
val activity = activityReference.get()
if (activity == null || activity.isFinishing) return
// access Activity member variables or modify the activity's UI
activity.progressBar.visibility = View.GONE
activity.textView.text = result.let { it }
activity.myVariable = 100
}
override fun onProgressUpdate(vararg text: String?) {
val activity = activityReference.get()
if (activity == null || activity.isFinishing) return
Toast.makeText(activity, text.firstOrNull(), Toast.LENGTH_SHORT).show()
}
}
}
}
No comments:
Post a Comment