AsyncTask has been deprecated in Android 11.
There are a lot of alternatives to AsyncTask like RxJava, Services, Coroutines, etc.
CoroutineTask is a utility that has the same structure as AsyncTask but uses Kotlin Coroutines underneath
In the same way you use AsyncTask, just replace the keyword AsyncTask with CoroutineTask
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
CoroutineTask.kt
val asyncTask = object: CoroutineTask <String?, Int?, String?>() {
override fun onPreExecute() {}
override fun doInBackground(vararg params: String?): String? {
for (i in 0. .100000) {
// optional, publishing the progress
val progress: Int = (i / 1000)
publishProgress(progress)
}
return params[0]
}
override fun onPostExecute(result: String?) {
Toast.makeText(applicationContext, result, LENGTH_LONG).show()
}
override fun onCancelled() {}
override fun onProgressUpdate(progress: Int?) {
Log.i("progress: ", "" + progress)
}
}
asyncTask.execute("background work completed")
val asyncTask: CoroutineTask <Void?, Void?, String?> =
object : CoroutineTask <Void?, Void?, String?>() {
override fun onPreExecute() {
// do something
}
override fun doInBackground(vararg params: Void?): String? {
return ""
}
override fun onPostExecute(result: String?) {
// do something
}
override fun onCancelled() {
// optional
}
}
asyncTask.execute()
CoroutineTask asyncTask = new CoroutineTask <Void, Void, String>() {
@Override
protected void onPreExecute() {}
@Override
protected String doInBackground(Void... params) {
return "";
}
@Override
protected void onPostExecute(String result) {}
@Override
protected void onCancelled() {
// optional
}
};
asyncTask.execute();
CoroutineTask asyncTask = new CoroutineTask <Void, Void, String>() {
@Override
protected void onPreExecute() {}
@Override
protected String doInBackground(Void... params) {
return "";
}
@Override
protected void onPostExecute(String result) {}
};
asyncTask.execute();
asyncTask.cancel(true);