-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.kt
62 lines (56 loc) · 1.9 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.example.mycoroutines
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
class MainActivity : AppCompatActivity() {
val tag = "MainActivityThraed"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
val textView: TextView = findViewById(R.id.tv)
var job = GlobalScope.launch(Dispatchers.IO) {
repeat(5) {
delay(1000L)
Log.d(tag, "HI REPEATING")
textView.setText("HI")
}
}
runBlocking {
delay(2000L)
job.join()
// job.cancel()
Log.d(tag, "Main Thread is Continuing")
}
// GlobalScope.launch(Dispatchers.IO) {
// val networkCall = doNetworkCall()
// Log.d(tag, "Coroutines says hello from thraed1 " + Thread.currentThread().name)
//
// withContext(Dispatchers.Main) {
// Log.d(tag, "Coroutines says hello from thraed2 " + Thread.currentThread().name)
// textView.setText(networkCall)
//
// Toast.makeText(
// applicationContext,
// "Welcome to Kotlin Android!" + networkCall,
// Toast.LENGTH_SHORT
// ).show()
//
// }
// }
// Log.d(tag, "Coroutines says hello from thraed3 " + Thread.currentThread().name)
}
suspend fun doNetworkCall(): String {
delay(3000L)
return "HI"
}
}