Skip to content

About

Android 电池信息(含循环次数)

Resources

Stars

4 stars

Watchers

0 watching

Forks

Repository files navigation

获取电池信息示例(Kotlin + Jetpack Compose)

下面的示例展示了如何在 Android 上获取电池电量、当前容量和预估满电容量(当前容量/当前百分比),并在 Compose 中显示:

@Composable
fun batteryInfoView(context: Context) {
    val batteryInfo = getBatteryInfo(context)

    Text(
        text = "Battery: ${batteryInfo.first}% (${batteryInfo.second} mAh)",
        fontSize = 16.sp,
        fontWeight = FontWeight.Medium,
        color = Color(0xFF4CAF50)
    )

    Spacer(modifier = Modifier.height(4.dp))

    Text(
        text = "Total Capacity: ${batteryInfo.third} mAh",
        fontSize = 14.sp,
        color = Color.Gray
    )
}

private fun getBatteryInfo(context: Context): Triple<Int, Int, Int> {
    val batteryManager = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
    val percentage = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
    
    // 获取当前电量(微安时)并转换为毫安时
    val chargeCounter = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER)
    val mAh = chargeCounter / 1000
    
    // 计算总容量:当前毫安时 / 百分比 * 100
    val totalCapacity = if (percentage > 0) (mAh * 100) / percentage else 0
    
    return Triple(percentage, mAh, totalCapacity)
}

About

Android 电池信息(含循环次数)

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages