How to get Executor for main thread on API level < 28

后端 未结 3 1877
梦谈多话
梦谈多话 2021-02-07 16:25

On API level 28(Pie) a new method is introduced in the Context class to get Executor for the main thread getMainExecutor().

How to get this executor on API

3条回答
  •  感情败类
    2021-02-07 17:00

    You can use new HandlerExecutor(Looper.getMainLooper()); from com.google.android.gms.common.util.concurrent.HandlerExecutor...in the end it's the same answer as atarasenko.

    I added an extension in Kotlin for that:

    fun Context.mainExecutor(): Executor {
        return if (VERSION.SDK_INT >= VERSION_CODES.P) {
            mainExecutor
        } else {
            HandlerExecutor(mainLooper)
        }
    }
    

提交回复
热议问题