pull/1854/head
kunfei 3 years ago
parent 13a83202d7
commit 254a216e8a
  1. 23
      app/src/main/java/io/legado/app/receiver/NetworkChangedReceiver.kt
  2. 14
      app/src/main/java/io/legado/app/receiver/TimeBatteryReceiver.kt
  3. 4
      app/src/main/java/io/legado/app/service/WebService.kt
  4. 9
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt

@ -0,0 +1,23 @@
package io.legado.app.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
/**
* 监测网络变化
*/
class NetworkChangedReceiver : BroadcastReceiver() {
val filter = IntentFilter().apply {
addAction("android.net.conn.CONNECTIVITY_CHANGE")
addAction("android.net.wifi.WIFI_STATE_CHANGED")
addAction("android.net.wifi.STATE_CHANGE")
}
override fun onReceive(context: Context?, intent: Intent?) {
}
}

@ -11,17 +11,9 @@ import io.legado.app.utils.postEvent
class TimeBatteryReceiver : BroadcastReceiver() {
companion object {
fun register(context: Context): TimeBatteryReceiver {
val receiver = TimeBatteryReceiver()
val filter = IntentFilter()
filter.addAction(Intent.ACTION_TIME_TICK)
filter.addAction(Intent.ACTION_BATTERY_CHANGED)
context.registerReceiver(receiver, filter)
return receiver
}
val filter = IntentFilter().apply {
addAction(Intent.ACTION_TIME_TICK)
addAction(Intent.ACTION_BATTERY_CHANGED)
}
override fun onReceive(context: Context?, intent: Intent?) {

@ -10,6 +10,7 @@ import io.legado.app.constant.AppConst
import io.legado.app.constant.EventBus
import io.legado.app.constant.IntentAction
import io.legado.app.constant.PreferKey
import io.legado.app.receiver.NetworkChangedReceiver
import io.legado.app.utils.*
import io.legado.app.web.HttpServer
import io.legado.app.web.WebSocketServer
@ -35,6 +36,7 @@ class WebService : BaseService() {
private var httpServer: HttpServer? = null
private var webSocketServer: WebSocketServer? = null
private var notificationContent = ""
private val networkChangedReceiver = NetworkChangedReceiver()
override fun onCreate() {
super.onCreate()
@ -42,6 +44,7 @@ class WebService : BaseService() {
notificationContent = getString(R.string.service_starting)
upNotification()
upTile(true)
registerReceiver(networkChangedReceiver, networkChangedReceiver.filter)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@ -55,6 +58,7 @@ class WebService : BaseService() {
override fun onDestroy() {
super.onDestroy()
unregisterReceiver(networkChangedReceiver)
isRun = false
if (httpServer?.isAlive == true) {
httpServer?.stop()

@ -148,8 +148,8 @@ class ReadBookActivity : BaseReadBookActivity(),
set(value) {
field = value && isShowingSearchResult
}
private val timeBatteryReceiver = TimeBatteryReceiver()
private var screenTimeOut: Long = 0
private var timeBatteryReceiver: TimeBatteryReceiver? = null
private var loadStates: Boolean = false
override val pageFactory: TextPageFactory get() = binding.readView.pageFactory
override val headerHeight: Int get() = binding.readView.curPage.headerHeight
@ -185,7 +185,7 @@ class ReadBookActivity : BaseReadBookActivity(),
super.onResume()
ReadBook.readStartTime = System.currentTimeMillis()
upSystemUiVisibility()
timeBatteryReceiver = TimeBatteryReceiver.register(this)
registerReceiver(timeBatteryReceiver, timeBatteryReceiver.filter)
binding.readView.upTime()
}
@ -194,10 +194,7 @@ class ReadBookActivity : BaseReadBookActivity(),
autoPageStop()
backupJob?.cancel()
ReadBook.saveRead()
timeBatteryReceiver?.let {
unregisterReceiver(it)
timeBatteryReceiver = null
}
unregisterReceiver(timeBatteryReceiver)
upSystemUiVisibility()
if (!BuildConfig.DEBUG) {
ReadBook.uploadProgress()

Loading…
Cancel
Save