67.10 Using a Coroutine for the Service Task
As outlined in “A Basic Overview of Threads and AsyncTasks”, when an Android application is first started, the runtime system creates a single thread in which all application components will run by default. This thread is generally referred to as the main thread. The primary role of the main thread is to handle the user interface in terms of event handling and interaction with views in the user interface. Any additional components that are started within the application will, by default, also run on the main thread.
A very simple solution to this problem involves performing the service task within a Kotlin Coroutine. Start by editing the Gradle Scripts -> build.gradle (Module: ServiceExample.app) file to add the following lines to the dependencies section):
dependencies {
.
.
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
implementation...