-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init
- Loading branch information
0 parents
commit 5bc76e6
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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" | ||
} | ||
} |