From 84fa663f7b83d482c1715c7abbde01292b5eb734 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 26 Aug 2021 22:02:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=9C=97=E8=AF=BB=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E8=AE=A1=E6=97=B6=E6=96=B9=E6=B3=95,handler.removeCal?= =?UTF-8?q?lbacks=E6=9C=89=E6=97=B6=E5=80=99=E4=BC=9A=E5=A4=B1=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/service/BaseReadAloudService.kt | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt index 7f50e1546..a30358f6b 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -29,6 +29,10 @@ import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.postEvent import io.legado.app.utils.toastOnUi +import kotlinx.coroutines.Job +import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch import splitties.init.appCtx abstract class BaseReadAloudService : BaseService(), @@ -56,7 +60,7 @@ abstract class BaseReadAloudService : BaseService(), internal var readAloudNumber: Int = 0 internal var textChapter: TextChapter? = null internal var pageIndex = 0 - private val dsRunnable: Runnable = Runnable { doDs() } + private var dsJob: Job? = null override fun onCreate() { super.onCreate() @@ -69,7 +73,7 @@ abstract class BaseReadAloudService : BaseService(), initBroadcastReceiver() upNotification() upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING) - startDs() + doDs() } override fun onDestroy() { @@ -149,7 +153,7 @@ abstract class BaseReadAloudService : BaseService(), upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED) postEvent(EventBus.ALOUD_STATE, Status.PAUSE) ReadBook.uploadProgress() - startDs() + doDs() } @CallSuper @@ -184,7 +188,7 @@ abstract class BaseReadAloudService : BaseService(), private fun setTimer(minute: Int) { timeMinute = minute - startDs() + doDs() } private fun addTimer() { @@ -194,32 +198,34 @@ abstract class BaseReadAloudService : BaseService(), timeMinute += 10 if (timeMinute > 180) timeMinute = 180 } - startDs() - } - - private fun startDs() { - postEvent(EventBus.TTS_DS, timeMinute) - upNotification() - handler.removeCallbacks(dsRunnable) - handler.postDelayed(dsRunnable, 60000) + doDs() } /** * 定时 */ + @Synchronized private fun doDs() { - handler.removeCallbacks(dsRunnable) - if (!pause) { - if (timeMinute >= 0) { - timeMinute-- - } - if (timeMinute == 0) { - ReadAloud.stop(this) - } - } postEvent(EventBus.TTS_DS, timeMinute) upNotification() - handler.postDelayed(dsRunnable, 60000) + dsJob?.cancel() + dsJob = launch { + while (isActive) { + delay(60000) + if (isActive) { + if (!pause) { + if (timeMinute >= 0) { + timeMinute-- + } + if (timeMinute == 0) { + ReadAloud.stop(this@BaseReadAloudService) + } + } + postEvent(EventBus.TTS_DS, timeMinute) + upNotification() + } + } + } } /**