Skip to content

Commit 0b0222e

Browse files
committed
asynctask code added
1 parent 8856175 commit 0b0222e

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

asynctask/CustomAsyncTask.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//first of generic types are params, second progress and third is final result
2+
private class CustomAsyncTask : AsyncTask<String, Int, String>() {
3+
4+
override fun onPreExecute() {
5+
//prepare before running background job like show progress dialog
6+
}
7+
8+
override fun doInBackground(vararg params: String): String {
9+
//some background job with passed params like URLs
10+
var total = ""
11+
for((counter, url) in params.withIndex()) {
12+
//download info from url
13+
val result = "result from : $url\n"
14+
total = total.plus(url)
15+
publishProgress(counter) //inform that some part of full request if completed
16+
}
17+
return total
18+
}
19+
20+
override fun onProgressUpdate(vararg values: Int?) {
21+
//show some progress updates based on Int from publishProgress
22+
}
23+
24+
override fun onPostExecute(result: String) {
25+
//do something on main thread when background job finished like update view
26+
}
27+
28+
override fun onCancelled() {
29+
//stop and clear or show some message
30+
}
31+
}
32+
33+
//to start just call
34+
//CustomAsyncTask().execute("url1", "url2", "url3")

asynctask/MainActivity.kt

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
class MainActivity : AppCompatActivity(), Listener {
2+
3+
//reference to task allows to manage it's lifecycle
4+
private var asyncTask : SaferAsyncTask? = null
5+
6+
override fun onCreate(savedInstanceState: Bundle?) {
7+
super.onCreate(savedInstanceState)
8+
setContentView(R.layout.activity_main)
9+
10+
button.setOnClickListener {
11+
//AsyncTask is single shot, so create new task every time
12+
if(asyncTask == null || asyncTask?.status != AsyncTask.Status.RUNNING) {
13+
asyncTask = SaferAsyncTask(this)
14+
asyncTask?.execute("url1", "url2", "url3")
15+
}
16+
}
17+
}
18+
19+
override fun onDestroy() {
20+
//stop the task when exit from Activity
21+
if(asyncTask?.status == AsyncTask.Status.RUNNING)
22+
asyncTask?.cancel(true)
23+
super.onDestroy()
24+
}
25+
26+
override fun onStarting() {
27+
//create progress dialog
28+
}
29+
30+
override fun onProgress(progress: Int) {
31+
//update progress dialog
32+
}
33+
34+
override fun onFinished(result: String) {
35+
//close progress dialog
36+
//show the result
37+
}
38+
39+
override fun onCancel() {
40+
//close progress dialog
41+
//show error message
42+
}
43+
44+
private class SaferAsyncTask(listener : Listener) : AsyncTask<String, Int, String>() {
45+
46+
private val reference = WeakReference<Listener>(listener)
47+
48+
override fun onPreExecute() {
49+
reference.get()?.onStarting()
50+
}
51+
52+
override fun doInBackground(vararg params: String): String {
53+
var total = ""
54+
for((counter, url) in params.withIndex()) {
55+
val result = "result from : $url\n"
56+
total = total.plus(result)
57+
publishProgress(counter) //inform that some part of full request if completed
58+
}
59+
return total
60+
}
61+
62+
override fun onProgressUpdate(vararg values: Int?) {
63+
values[0]?.let { reference.get()?.onProgress(it) }
64+
}
65+
66+
override fun onPostExecute(result: String) {
67+
reference.get()?.onFinished(result)
68+
}
69+
70+
override fun onCancelled() {
71+
reference.get()?.onCancel()
72+
}
73+
}
74+
}
75+
76+
private interface Listener {
77+
fun onStarting()
78+
fun onProgress(progress : Int)
79+
fun onFinished(result : String)
80+
fun onCancel()
81+
}

asynctask/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# asynctask
2+
This is repository of http://androidcode.pl blog. It shows uses AsyncTask in Android. It is a part of Background processing - AsyncTask post in the blog.

0 commit comments

Comments
 (0)