Class Battery
java.lang.Object
com.codename1.system.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 Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceNotified when the battery level, state or low power mode changes. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intHow often, in milliseconds, registered listeners are checked.static final intPlugged in and charging.static final intPlugged in and full.static final intThe platform cannot tell the battery state.static final intRunning on battery. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidAdds a listener and starts checking for changes.static voidCompares the current values with the last ones and notifies the listeners when one changed.static intgetLevel()The battery charge from 0 to 100, or -1 when unknown.static intgetState()The battery state.static booleanTrue when the device runs in low power mode (iOS) or battery saver (Android).static booleanTrue when the platform reports a battery level.static voidRemoves a listener.static voidsetImplementation(com.codename1.impl.CodenameOneImplementation impl) Invoked internally from Display, this method is for internal use only
-
Field Details
-
STATE_UNKNOWN
public static final int STATE_UNKNOWNThe platform cannot tell the battery state.- See Also:
-
STATE_UNPLUGGED
public static final int STATE_UNPLUGGEDRunning on battery.- See Also:
-
STATE_CHARGING
public static final int STATE_CHARGINGPlugged in and charging.- See Also:
-
STATE_FULL
public static final int STATE_FULLPlugged in and full.- See Also:
-
POLL_INTERVAL
public static final int POLL_INTERVALHow often, in milliseconds, registered listeners are checked.- See Also:
-
-
Method Details
-
setImplementation
public static void setImplementation(com.codename1.impl.CodenameOneImplementation impl) Invoked internally from Display, this method is for internal use only
Parameters
impl: implementation instance
-
isSupported
public static boolean isSupported()True when the platform reports a battery level.
Returns
true when
#getLevel()answers a real value -
getLevel
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_constants -
isLowPowerMode
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
Adds a listener and starts checking for changes.
Parameters
l: the listener
-
removeListener
Removes a listener. Checking stops with the last one.
Parameters
l: 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 throughCN#callSerially.
-