desc:电池一通改

main
xiaowusky 1 year ago
parent 3ad25de4fe
commit 2bf63533ec

@ -28,6 +28,7 @@
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACTION_POWER_CONNECTED" />
<uses-feature
android:glEsVersion="0x00020000"

@ -59,10 +59,10 @@ class CommonTopBar : LinearLayout {
}
private val batteryCallback = object : BatteryHelper.OnBatteryLevelCallback {
override fun onLevel(level: Int) {
Log.i(this@CommonTopBar.javaClass.name, "batteryCallback onLevel = $level")
override fun onLevel(level: Int, charging: Boolean) {
Log.i(this@CommonTopBar.javaClass.name, "batteryCallback onLevel = $level charging = $charging")
mBinding?.root?.post {
mBinding?.battery?.setImageLevel(level)
mBinding?.battery?.setImageLevel(if (charging) 200 else level)
mBinding?.batteryTxt?.text = "${level.toString()}%"
dealForNet4()
}

@ -6,37 +6,82 @@ import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.util.Log
import com.common.commonlib.CommonApplication
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
object BatteryHelper {
private var callbacks = mutableListOf<OnBatteryLevelCallback>()
private var mLevel = -1;
private var batteryManager: BatteryManager? = null
// private var charging = false;
private val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val level = intent?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
mLevel = level!!
Log.i(this@BatteryHelper.javaClass.name, "onReceive level = $level")
callbacks.forEach {
if (level != null) {
it.onLevel(level)
val action = intent?.action
if (Intent.ACTION_BATTERY_CHANGED == action) {
val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)!!
notifyUser(level)
mLevel = level
Log.i(this@BatteryHelper.javaClass.name, "onReceive level = $level")
callbacks.forEach {
it.onLevel(level, batteryManager?.isCharging == true)
}
} /*else if (Intent.ACTION_POWER_CONNECTED == action) {
charging = true
} else if (Intent.ACTION_POWER_DISCONNECTED == action) {
charging = false
}*/
}
}
private fun notifyUser(level: Int) {
val tip = if (level < 10) {
if (mLevel >= 10 || mLevel == -1) {
"电池电量低于10%"
} else {
""
}
} else if (level < 20) {
if (mLevel >= 20 || mLevel == -1) {
"电池电量低与20%"
} else {
""
}
} else if (level == 100) {
if (mLevel < 100) {
"充电完成"
} else {
""
}
} else {
""
}
if (tip.isNotEmpty()) {
GlobalScope.launch(Dispatchers.Main) {
CommonApplication.getContext()?.showToast(tip)
}
}
}
fun init(context: Context) {
watchBattery(context)
batteryManager = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
}
private fun watchBattery(context: Context) {
val filter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
// filter.addAction(Intent.ACTION_POWER_CONNECTED)
// filter.addAction(Intent.ACTION_POWER_DISCONNECTED)
context.registerReceiver(receiver, filter)
}
fun addCallBack(callBack: OnBatteryLevelCallback) {
callbacks.add(callBack)
if (mLevel != -1) {
callBack.onLevel(mLevel)
callBack.onLevel(mLevel, batteryManager?.isCharging == true)
}
}
@ -50,6 +95,6 @@ object BatteryHelper {
}
interface OnBatteryLevelCallback {
fun onLevel(level: Int)
fun onLevel(level: Int, charging: Boolean)
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

@ -25,4 +25,8 @@
android:drawable="@drawable/battery_100"
android:maxLevel="100"
android:minLevel="81" />
<item
android:drawable="@drawable/battery_charging"
android:maxLevel="200"
android:minLevel="200" />
</level-list>
Loading…
Cancel
Save