Skip to main content

Posts

Showing posts with the label Recheck

android kotlin - Coroutines async

MainActivity.kt package com.example.coroutine import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* import kotlinx.coroutines.* import java.io.BufferedReader import java.io.InputStreamReader import java.net.URL class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // url to get html val url:URL = URL("https://developer.android.com/") button.setOnClickListener { // creates a coroutine and returns its future result // as an implementation of deferred // async task to get html as string from url val result: Deferred = GlobalScope.async { url.getHtml() } // global scope is used to launch top-level coroutines which are // operating on the wh...

android kotlin - Coroutines delay

MainActivity.kt package com.example.coroutine import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_main.* import kotlinx.coroutines.* import kotlin.random.Random class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // show random numbers periodically button.setOnClickListener { GlobalScope.launch(Dispatchers.Main) { doTask() } } } private suspend fun doTask(){ textView.text = "Random numbers..." for (i in 12 downTo 0){ // delays coroutine for a given time without blocking // a thread and resumes it after a specified time delay(1000) // time in milliseconds // show next random number after delay textView.append("\n" +...