Class Battery

java.lang.Object
com.codename1.system.Battery

public final class Battery extends Object

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 Classes
    Modifier and Type
    Class
    Description
    static interface 
    Notified when the battery level, state or low power mode changes.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    How often, in milliseconds, registered listeners are checked.
    static final int
    Plugged in and charging.
    static final int
    Plugged in and full.
    static final int
    The platform cannot tell the battery state.
    static final int
    Running on battery.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Adds a listener and starts checking for changes.
    static void
    Compares the current values with the last ones and notifies the listeners when one changed.
    static int
    The battery charge from 0 to 100, or -1 when unknown.
    static int
    The battery state.
    static boolean
    True when the device runs in low power mode (iOS) or battery saver (Android).
    static boolean
    True when the platform reports a battery level.
    static void
    Removes a listener.
    static void
    setImplementation(com.codename1.impl.CodenameOneImplementation impl)
    Invoked internally from Display, this method is for internal use only

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • STATE_UNKNOWN

      public static final int STATE_UNKNOWN
      The platform cannot tell the battery state.
      See Also:
    • STATE_UNPLUGGED

      public static final int STATE_UNPLUGGED
      Running on battery.
      See Also:
    • STATE_CHARGING

      public static final int STATE_CHARGING
      Plugged in and charging.
      See Also:
    • STATE_FULL

      public static final int STATE_FULL
      Plugged in and full.
      See Also:
    • POLL_INTERVAL

      public static final int POLL_INTERVAL
      How 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

      public static void addListener(Battery.Listener l)

      Adds a listener and starts checking for changes.

      Parameters
      • l: the listener
    • removeListener

      public static void removeListener(Battery.Listener l)

      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 through CN#callSerially.