pull/32/head
kunfei 5 years ago
parent 1ca339c194
commit ff95f864ef
  1. 2
      app/src/main/java/io/legado/app/constant/Bus.kt
  2. 7
      app/src/main/java/io/legado/app/receiver/TimeElectricityReceiver.kt
  3. 16
      app/src/main/java/io/legado/app/utils/BatteryUtils.kt

@ -6,4 +6,6 @@ object Bus {
const val ALOUD_STATE = "aloud_state" const val ALOUD_STATE = "aloud_state"
const val TTS_START = "ttsStart" const val TTS_START = "ttsStart"
const val TTS_RANGE_START = "readAloudNumber" const val TTS_RANGE_START = "readAloudNumber"
const val BATTERY_CHANGED = "batteryChanged"
const val TIME_CHANGED = "timeChanged"
} }

@ -4,6 +4,9 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.os.BatteryManager
import io.legado.app.constant.Bus
import io.legado.app.utils.postEvent
class TimeElectricityReceiver : BroadcastReceiver() { class TimeElectricityReceiver : BroadcastReceiver() {
@ -25,9 +28,11 @@ class TimeElectricityReceiver : BroadcastReceiver() {
intent?.action?.let { intent?.action?.let {
when (it) { when (it) {
Intent.ACTION_TIME_TICK -> { Intent.ACTION_TIME_TICK -> {
postEvent(Bus.TIME_CHANGED, "")
} }
Intent.ACTION_BATTERY_CHANGED -> { Intent.ACTION_BATTERY_CHANGED -> {
val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
postEvent(Bus.BATTERY_CHANGED, level)
} }
} }
} }

@ -0,0 +1,16 @@
package io.legado.app.utils
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
object BatteryUtils {
fun getLevel(context: Context): Int {
val iFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
val batteryStatus = context.registerReceiver(null, iFilter)
return batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1
}
}
Loading…
Cancel
Save