Skip to content

Instantly share code, notes, and snippets.

@af2905
Last active October 24, 2023 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save af2905/6614f9aef7f1868d1a175719a2e44138 to your computer and use it in GitHub Desktop.
Save af2905/6614f9aef7f1868d1a175719a2e44138 to your computer and use it in GitHub Desktop.
receive_data_on_ui_thread
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
@OptIn(ExperimentalTime::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
this.runOnUiThread {
val (result: String, duration: Duration) = measureTimedValue { receiveData() }
Log.d("TAG", "$result after ${duration.toDouble(DurationUnit.MILLISECONDS)} ms.")
showToast(context = this, message = result)
}
Log.d("TAG", "onCreate completed")
}
private fun showToast(context: Context, message: String) {
val toast = Toast.makeText(context, message, Toast.LENGTH_SHORT)
toast.show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment