diff --git a/app/src/main/java/io/legado/app/constant/Bus.kt b/app/src/main/java/io/legado/app/constant/Bus.kt index e680d1e8c..6d7c7b872 100644 --- a/app/src/main/java/io/legado/app/constant/Bus.kt +++ b/app/src/main/java/io/legado/app/constant/Bus.kt @@ -6,4 +6,6 @@ object Bus { const val ALOUD_STATE = "aloud_state" const val TTS_START = "ttsStart" const val TTS_RANGE_START = "readAloudNumber" + const val BATTERY_CHANGED = "batteryChanged" + const val TIME_CHANGED = "timeChanged" } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/receiver/TimeElectricityReceiver.kt b/app/src/main/java/io/legado/app/receiver/TimeElectricityReceiver.kt index 59d0424ee..613db4fe4 100644 --- a/app/src/main/java/io/legado/app/receiver/TimeElectricityReceiver.kt +++ b/app/src/main/java/io/legado/app/receiver/TimeElectricityReceiver.kt @@ -4,6 +4,9 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter +import android.os.BatteryManager +import io.legado.app.constant.Bus +import io.legado.app.utils.postEvent class TimeElectricityReceiver : BroadcastReceiver() { @@ -25,9 +28,11 @@ class TimeElectricityReceiver : BroadcastReceiver() { intent?.action?.let { when (it) { Intent.ACTION_TIME_TICK -> { - + postEvent(Bus.TIME_CHANGED, "") } Intent.ACTION_BATTERY_CHANGED -> { + val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) + postEvent(Bus.BATTERY_CHANGED, level) } } } diff --git a/app/src/main/java/io/legado/app/utils/BatteryUtils.kt b/app/src/main/java/io/legado/app/utils/BatteryUtils.kt new file mode 100644 index 000000000..c7d49a3aa --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/BatteryUtils.kt @@ -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 + } +}