Help understanding coroutines android kotlin

My program works great if I use the following code however I know the calling GlobalScope is bad but I can’t make anything else work.
Please help me fix the following code so that it’s written cleanly with co-routines I must use a coroutine to do the work.


//periodic polling
val mainHandler = Handler(Looper.getMainLooper())
var oneTime0=true : Runnable {
    override fun run() {
        mainHandler.postDelayed(this, 6000)

        for (i3 in 0..20) {
           GlobalScope.launch{pollImageSlotMethod0(context0, act0)}


The Coroutines Guide has a lot of useful info on coroutines.

Take a look here for how to launch a periodic event: Coroutine Basics - Global coroutines are like daemon threads

Here for how to setup a coroutine that linked to your Activity lifecycle: Coroutine Context and Dispatchers - Coroutine scope

If you use Android’s Architectural Components, then you can just get a CoroutineScope from your ViewModel or LifecycleOwner with some ktx libraries: Use Kotlin coroutines with Architecture components