Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class Battery
- Object
- Battery
Battery level, charging state and low power mode. None of it needs a permission on any platform.
Listeners are checked on a timer while at least one is registered, and are called on the EDT only when a value changed.
Log.p("Battery at " + Battery.getLevel() + "%");
Battery.addListener(new Battery.Listener() {
public void batteryChanged(int level, int state, boolean lowPower) {
Log.p("Battery now " + level + "%, low power " + lowPower);
}
});Nested types
interface Battery.Listener | Notified when the battery level, state or low power mode changes. |
Fields
public static final int STATE_UNKNOWN = 0 | The platform cannot tell the battery state. |
public static final int STATE_UNPLUGGED = 1 | Running on battery. |
public static final int STATE_CHARGING = 2 | Plugged in and charging. |
public static final int STATE_FULL = 3 | Plugged in and full. |
public static final int POLL_INTERVAL = 30000 | How often, in milliseconds, registered listeners are checked. |
Methods
public static void setImplementation(CodenameOneImplementation impl) | Invoked internally from Display, this method is for internal use only |
public static boolean isSupported() | True when the platform reports a battery level. |
public static int getLevel() | The battery charge from 0 to 100, or -1 when unknown. |
public static int getState() | The battery state. |
public static boolean isLowPowerMode() | True when the device runs in low power mode (iOS) or battery saver (Android). |
public static void addListener(Battery.Listener l) | Adds a listener and starts checking for changes. |
public static void removeListener(Battery.Listener l) | Removes a listener. |
public static void checkForChanges() | Compares the current values with the last ones and notifies the listeners when one changed. |
Inherited methods
Field details
STATE_UNKNOWN
public static final int STATE_UNKNOWN = 0The platform cannot tell the battery state.
STATE_UNPLUGGED
public static final int STATE_UNPLUGGED = 1Running on battery.
STATE_CHARGING
public static final int STATE_CHARGING = 2Plugged in and charging.
STATE_FULL
public static final int STATE_FULL = 3Plugged in and full.
POLL_INTERVAL
public static final int POLL_INTERVAL = 30000How often, in milliseconds, registered listeners are checked.
Method details
setImplementation
public static void setImplementation(CodenameOneImplementation impl)Invoked internally from Display, this method is for internal use only
Parameters
implCodenameOneImplementation- implementation instance
isSupported
public static boolean isSupported()True when the platform reports a battery level.
Returns
true when
#getLevel() answers a real valuegetLevel
public static int getLevel()The battery charge from 0 to 100, or -1 when unknown.
Returns
the charge percentage or -1
getState
public static int getState()The battery state.
Returns
one of the
STATE_ constantsisLowPowerMode
public static boolean isLowPowerMode()True when the device runs in low power mode (iOS) or battery saver
(Android).
Returns
true in low power mode
addListener
public static void addListener(Battery.Listener l)Adds a listener and starts checking for changes.
Parameters
lBattery.Listener- the listener
removeListener
public static void removeListener(Battery.Listener l)Removes a listener. Checking stops with the last one.
Parameters
lBattery.Listener- the listener
checkForChanges
public static void checkForChanges()Compares the current values with the last ones and notifies the
listeners when one changed. Runs on the EDT; ports that receive
battery events may call it directly through
CN#callSerially.