修改定时计时方法,handler.removeCallbacks有时候会失效

pull/1275/head
gedoor 3 years ago
parent 84fa663f7b
commit 5e362db480
  1. 3
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  2. 7
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
  3. 49
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt

@ -8,8 +8,6 @@ import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.media.AudioManager import android.media.AudioManager
import android.os.Handler
import android.os.Looper
import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat import android.support.v4.media.session.PlaybackStateCompat
import androidx.annotation.CallSuper import androidx.annotation.CallSuper
@ -48,7 +46,6 @@ abstract class BaseReadAloudService : BaseService(),
} }
} }
internal val handler = Handler(Looper.getMainLooper())
private lateinit var audioManager: AudioManager private lateinit var audioManager: AudioManager
private var mFocusRequest: AudioFocusRequestCompat? = null private var mFocusRequest: AudioFocusRequestCompat? = null
private var broadcastReceiver: BroadcastReceiver? = null private var broadcastReceiver: BroadcastReceiver? = null

@ -10,8 +10,10 @@ import io.legado.app.model.ReadBook
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.service.help.ReadAloud import io.legado.app.service.help.ReadAloud
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.io.File import java.io.File
import java.io.FileDescriptor import java.io.FileDescriptor
import java.io.FileInputStream import java.io.FileInputStream
@ -238,7 +240,8 @@ class HttpReadAloudService : BaseReadAloudService(),
if (what == -38 && extra == 0) { if (what == -38 && extra == 0) {
return true return true
} }
handler.postDelayed({ launch {
delay(100)
readAloudNumber += contentList[nowSpeak].length + 1 readAloudNumber += contentList[nowSpeak].length + 1
if (nowSpeak < contentList.lastIndex) { if (nowSpeak < contentList.lastIndex) {
nowSpeak++ nowSpeak++
@ -246,7 +249,7 @@ class HttpReadAloudService : BaseReadAloudService(),
} else { } else {
nextChapter() nextChapter()
} }
}, 50) }
return true return true
} }

@ -5,8 +5,6 @@ import android.app.Activity
import android.content.Intent import android.content.Intent
import android.content.res.Configuration import android.content.res.Configuration
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.* import android.view.*
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
@ -53,7 +51,9 @@ import io.legado.app.ui.replace.edit.ReplaceEditActivity
import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.ui.widget.dialog.TextDialog
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class ReadBookActivity : ReadBookBaseActivity(), class ReadBookActivity : ReadBookBaseActivity(),
@ -109,15 +109,9 @@ class ReadBookActivity : ReadBookBaseActivity(),
override val isInitFinish: Boolean get() = viewModel.isInitFinish override val isInitFinish: Boolean get() = viewModel.isInitFinish
override val isScroll: Boolean get() = binding.readView.isScroll override val isScroll: Boolean get() = binding.readView.isScroll
private val mHandler = Handler(Looper.getMainLooper()) private var keepScreenJon: Job? = null
private val keepScreenRunnable = Runnable { keepScreenOn(false) } private var autoPageJob: Job? = null
private val autoPageRunnable = Runnable { autoPagePlus() } private var backupJob: Job? = null
private val backupRunnable = Runnable {
if (!BuildConfig.DEBUG) {
ReadBook.uploadProgress()
Backup.autoBack(this)
}
}
override var autoPageProgress = 0 override var autoPageProgress = 0
override var isAutoPage = false override var isAutoPage = false
private var screenTimeOut: Long = 0 private var screenTimeOut: Long = 0
@ -169,7 +163,7 @@ class ReadBookActivity : ReadBookBaseActivity(),
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
autoPageStop() autoPageStop()
mHandler.removeCallbacks(backupRunnable) backupJob?.cancel()
ReadBook.saveRead() ReadBook.saveRead()
timeBatteryReceiver?.let { timeBatteryReceiver?.let {
unregisterReceiver(it) unregisterReceiver(it)
@ -629,7 +623,7 @@ class ReadBookActivity : ReadBookBaseActivity(),
launch { launch {
autoPageProgress = 0 autoPageProgress = 0
binding.readMenu.setSeekPage(ReadBook.durPageIndex()) binding.readMenu.setSeekPage(ReadBook.durPageIndex())
mHandler.postDelayed(backupRunnable, 600000) startBackupJob()
} }
} }
@ -692,13 +686,16 @@ class ReadBookActivity : ReadBookBaseActivity(),
override fun autoPageStop() { override fun autoPageStop() {
if (isAutoPage) { if (isAutoPage) {
isAutoPage = false isAutoPage = false
mHandler.removeCallbacks(autoPageRunnable) autoPageJob?.cancel()
binding.readMenu.setAutoPage(false) binding.readMenu.setAutoPage(false)
upScreenTimeOut() upScreenTimeOut()
} }
} }
private fun autoPagePlus() { private fun autoPagePlus() {
autoPageJob?.cancel()
autoPageJob = launch {
while (isActive) {
var delayMillis = ReadBookConfig.autoReadSpeed * 1000L / binding.readView.height var delayMillis = ReadBookConfig.autoReadSpeed * 1000L / binding.readView.height
var scrollOffset = 1 var scrollOffset = 1
if (delayMillis < 20) { if (delayMillis < 20) {
@ -707,8 +704,8 @@ class ReadBookActivity : ReadBookBaseActivity(),
scrollOffset = 20 / delayInt scrollOffset = 20 / delayInt
delayMillis = 20 delayMillis = 20
} }
mHandler.removeCallbacks(autoPageRunnable) delay(delayMillis)
if (!menuLayoutIsVisible) { if (!menuLayoutIsVisible && isActive) {
if (binding.readView.isScroll) { if (binding.readView.isScroll) {
binding.readView.curPage.scroll(-scrollOffset) binding.readView.curPage.scroll(-scrollOffset)
} else { } else {
@ -723,7 +720,8 @@ class ReadBookActivity : ReadBookBaseActivity(),
} }
} }
} }
mHandler.postDelayed(autoPageRunnable, delayMillis) }
}
} }
override fun openSourceEditActivity() { override fun openSourceEditActivity() {
@ -878,6 +876,13 @@ class ReadBookActivity : ReadBookBaseActivity(),
} }
} }
private fun startBackupJob() {
backupJob?.cancel()
backupJob = launch {
delay(120000)
}
}
override fun finish() { override fun finish() {
ReadBook.book?.let { ReadBook.book?.let {
if (!ReadBook.inBookshelf) { if (!ReadBook.inBookshelf) {
@ -897,7 +902,6 @@ class ReadBookActivity : ReadBookBaseActivity(),
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
mHandler.removeCallbacks(keepScreenRunnable)
textActionMenu.dismiss() textActionMenu.dismiss()
binding.readView.onDestroy() binding.readView.onDestroy()
ReadBook.msg = null ReadBook.msg = null
@ -975,17 +979,20 @@ class ReadBookActivity : ReadBookBaseActivity(),
* 重置黑屏时间 * 重置黑屏时间
*/ */
override fun screenOffTimerStart() { override fun screenOffTimerStart() {
keepScreenJon?.cancel()
keepScreenJon = launch {
if (screenTimeOut < 0) { if (screenTimeOut < 0) {
keepScreenOn(true) keepScreenOn(true)
return return@launch
} }
val t = screenTimeOut - sysScreenOffTime val t = screenTimeOut - sysScreenOffTime
if (t > 0) { if (t > 0) {
mHandler.removeCallbacks(keepScreenRunnable)
keepScreenOn(true) keepScreenOn(true)
mHandler.postDelayed(keepScreenRunnable, screenTimeOut) delay(screenTimeOut)
keepScreenOn(false)
} else { } else {
keepScreenOn(false) keepScreenOn(false)
} }
} }
}
} }
Loading…
Cancel
Save