Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
init
  • Loading branch information
swagatampanda3894 authored Jul 19, 2024
0 parents commit 5bc76e6
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions MainActivity.kt
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"
}
}

0 comments on commit 5bc76e6

Please sign in to comment.