|
|
|
@ -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)
|
|
|
|
|
}
|
|
|
|
|
}
|