Stay organized with collections
Save and categorize content based on your preferences.
BatteryState
abstract class BatteryState
The BatteryState class is a representation of a single battery on a device.
Summary
Constants |
static Int |
Battery is charging.
|
static Int |
Battery is discharging.
|
static Int |
Battery is full.
|
static Int |
Battery is connected to power but not charging.
|
static Int |
Battery status is unknown.
|
Public methods |
abstract Float |
Get remaining battery capacity as float percentage [0.
|
abstract Int |
Get the battery status.
|
abstract Boolean |
Check whether the hardware has a battery.
|
Constants
STATUS_CHARGING
static val STATUS_CHARGING: Int
Battery is charging.
Value: 2
STATUS_DISCHARGING
static val STATUS_DISCHARGING: Int
Battery is discharging.
Value: 3
STATUS_FULL
static val STATUS_FULL: Int
Battery is full.
Value: 5
STATUS_NOT_CHARGING
static val STATUS_NOT_CHARGING: Int
Battery is connected to power but not charging.
Value: 4
STATUS_UNKNOWN
static val STATUS_UNKNOWN: Int
Battery status is unknown.
Value: 1
Public constructors
BatteryState
BatteryState()
Public methods
getCapacity
abstract fun getCapacity(): Float
Get remaining battery capacity as float percentage [0.0f, 1.0f] of total capacity Returns NaN when battery capacity can't be read.
Return |
Float |
the battery capacity. Value is between -1.0f and 1.0f inclusive |
getStatus
abstract fun getStatus(): Int
Get the battery status.
isPresent
abstract fun isPresent(): Boolean
Check whether the hardware has a battery.
Return |
Boolean |
True if the hardware has a battery, else false. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# BatteryState\n\nAdded in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nBatteryState\n============\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/hardware/BatteryState \"View this page in Java\") \n\n```\nabstract class BatteryState\n```\n\n|---|------------------------------------|\n| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||\n| ↳ | [android.hardware.BatteryState](#) |\n\nThe BatteryState class is a representation of a single battery on a device.\n\nSummary\n-------\n\n| Constants ||\n|-----------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------|\n| static [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | [STATUS_CHARGING](#STATUS_CHARGING:kotlin.Int) Battery is charging. |\n| static [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | [STATUS_DISCHARGING](#STATUS_DISCHARGING:kotlin.Int) Battery is discharging. |\n| static [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | [STATUS_FULL](#STATUS_FULL:kotlin.Int) Battery is full. |\n| static [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | [STATUS_NOT_CHARGING](#STATUS_NOT_CHARGING:kotlin.Int) Battery is connected to power but not charging. |\n| static [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | [STATUS_UNKNOWN](#STATUS_UNKNOWN:kotlin.Int) Battery status is unknown. |\n\n| Public constructors ||\n|--------------------------------------------|---|\n| [BatteryState](#BatteryState())`()` \u003cbr /\u003e |\n\n| Public methods ||\n|---------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------|\n| abstract [Float](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-float/index.html) | [getCapacity](#getCapacity())`()` Get remaining battery capacity as float percentage \\[0. |\n| abstract [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | [getStatus](#getStatus())`()` Get the battery status. |\n| abstract [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | [isPresent](#isPresent())`()` Check whether the hardware has a battery. |\n\nConstants\n---------\n\n### STATUS_CHARGING\n\nAdded in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nstatic val STATUS_CHARGING: Int\n```\n\nBattery is charging. \n\n Value: 2\n\n### STATUS_DISCHARGING\n\nAdded in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nstatic val STATUS_DISCHARGING: Int\n```\n\nBattery is discharging. \n\n Value: 3\n\n### STATUS_FULL\n\nAdded in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nstatic val STATUS_FULL: Int\n```\n\nBattery is full. \n\n Value: 5\n\n### STATUS_NOT_CHARGING\n\nAdded in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nstatic val STATUS_NOT_CHARGING: Int\n```\n\nBattery is connected to power but not charging. \n\n Value: 4\n\n### STATUS_UNKNOWN\n\nAdded in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nstatic val STATUS_UNKNOWN: Int\n```\n\nBattery status is unknown. \n\n Value: 1\n\nPublic constructors\n-------------------\n\n### BatteryState\n\n```\nBatteryState()\n```\n\nPublic methods\n--------------\n\n### getCapacity\n\nAdded in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun getCapacity(): Float\n```\n\nGet remaining battery capacity as float percentage \\[0.0f, 1.0f\\] of total capacity Returns NaN when battery capacity can't be read.\n\n| Return ||\n|--------------------------------------------------------------------------------|-----------------------------------------------------------------|\n| [Float](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-float/index.html) | the battery capacity. Value is between -1.0f and 1.0f inclusive |\n\n### getStatus\n\nAdded in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun getStatus(): Int\n```\n\nGet the battery status.\n\n| Return ||\n|----------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | the battery status. Value is [android.hardware.BatteryState#STATUS_UNKNOWN](#STATUS_UNKNOWN:kotlin.Int), [android.hardware.BatteryState#STATUS_CHARGING](#STATUS_CHARGING:kotlin.Int), [android.hardware.BatteryState#STATUS_DISCHARGING](#STATUS_DISCHARGING:kotlin.Int), [android.hardware.BatteryState#STATUS_NOT_CHARGING](#STATUS_NOT_CHARGING:kotlin.Int), or [android.hardware.BatteryState#STATUS_FULL](#STATUS_FULL:kotlin.Int) |\n\n### isPresent\n\nAdded in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun isPresent(): Boolean\n```\n\nCheck whether the hardware has a battery.\n\n| Return ||\n|------------------------------------------------------------------------------------|-------------------------------------------------|\n| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | True if the hardware has a battery, else false. |"]]