From 358b3e26f4da87823c1c7f43d5ebc4012b749692 Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 20 Oct 2021 08:48:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/utils/HandlerUtils.kt | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/HandlerUtils.kt b/app/src/main/java/io/legado/app/utils/HandlerUtils.kt index 0eacea43b..3ce76e5ca 100644 --- a/app/src/main/java/io/legado/app/utils/HandlerUtils.kt +++ b/app/src/main/java/io/legado/app/utils/HandlerUtils.kt @@ -11,7 +11,7 @@ import kotlinx.coroutines.launch /** This main looper cache avoids synchronization overhead when accessed repeatedly. */ @JvmField -val mainLooper: Looper = Looper.getMainLooper()!! +val mainLooper: Looper = Looper.getMainLooper() @JvmField val mainThread: Thread = mainLooper.thread @@ -22,15 +22,17 @@ val isMainThread: Boolean inline get() = mainThread === Thread.currentThread() internal val currentThread: Any? inline get() = Thread.currentThread() -@JvmField -val mainHandler: Handler = if (SDK_INT >= 28) Handler.createAsync(mainLooper) else try { - Handler::class.java.getDeclaredConstructor( - Looper::class.java, - Handler.Callback::class.java, - Boolean::class.javaPrimitiveType // async - ).newInstance(mainLooper, null, true) -} catch (ignored: NoSuchMethodException) { - Handler(mainLooper) // Hidden constructor absent. Fall back to non-async constructor. +val mainHandler: Handler by lazy { + if (SDK_INT >= 28) Handler.createAsync(mainLooper) else try { + Handler::class.java.getDeclaredConstructor( + Looper::class.java, + Handler.Callback::class.java, + Boolean::class.javaPrimitiveType // async + ).newInstance(mainLooper, null, true) + } catch (ignored: NoSuchMethodException) { + // Hidden constructor absent. Fall back to non-async constructor. + Handler(mainLooper) + } } fun runOnUI(function: () -> Unit) {