MEEP 1.9950 (b67)
JCP EG draft 10-February-2014 08:45

Package javax.microedition.power

[OPTIONAL] Power management.

See: Description

Package javax.microedition.power Description

[OPTIONAL] Power management.

This package is a subpackage of javax.microedition.event. It provides a specialized event manager and events in order to take into account power management specific functionality.

Description

The Power Management package allows an application to monitor and respond to changes in the power states of the system. For example, the ON power state can be identified, and an application can respond to various conditions such as full power, power managed for efficiency, suspend, and sleep states.

Package Features

The Power Management API provides the following features:

Design Goals

The primary design goal for the Power Management API is to provide applications with accurate power state information as well as system activity so that it can respond appropriately as needed to save state or conclude transactions when state changes occur. Also it is a goal to avoid that a device prematurely terminates due to lack of power resources.

Package Architecture

When the PowerManager class is implemented, an application can use the addEventListener method of EventManager to register to receive notifications. The application implements the EventListener, an event listener interface. Applications can identify system state activities such as Sleep, Suspend or PmActive. The current state can be retrieved from the getPowerState() method as illustrated in the Power Management sample code below.

When the getEstimatedSecondsRemaining() and getBatteryLevel() methods are implemented by the PowerMonitor class, an application can retrieve the current time and battery level for the device. This is further illustrated in the Power Monitoring sample code below.

Power Management Sample Code

   import javax.microedition.event.*;
   import javax.microedition.power.*;
   import javax.microedition.midlet.*;

   // Wait for and react on changes in the power state.
   // Exit when the system is suspended or put to sleep.
   public class PowerSample2 extends MIDlet implements EventListener {

      // Get the system power manager.
      static PowerManager powermgr = PowerManager.getInstance();

      private PowerState state;

      // Program to get and handle the current power state.
      public void startApp() {

          // Register to be notified on power state changes
          powermgr.addEventListener(
              PowerStateEvent.POWER_STATE, this);

          // Get the current power state
          state = powermgr.getPowerState();

      }

      // Gets notified of Power Manager state changes.
      public void handleEvent(Event event) {
          PowerStateEvent psed = (PowerStateEvent)event;
          // ...
          // do this and that while being able to access previous and new
          // power state via psed.getPrevState() or
          // psed.getNewState(), respectively
          if (psed.getNewState() == PowerStateEvent.POWER_STATE_SUSPEND ||
              psed.getNewState() == PowerStateEvent.POWER_STATE_SLEEP) {
              notifyDestroyed();
          }
      }
  }
 

Power Monitoring Sample Code

 import javax.microedition.power.*;
 import javax.microedition.midlet.*;

 // Designate the battery level and time remaining.
 public class PowerSample1 extends MIDlet{

      // Get the system power manager.
      static PowerManager powermgr = PowerManager.getInstance();

      // Get and print the battery level and time remaining.
      public void startApp() {

          // Get the current time and battery level.
          int remaining = powermgr.getEstimatedSecondsRemaining();
          int level = powermgr.getBatteryLevel();

          if (remaining < 60) {
              // ...
              // do something useful
          }
      }
  }
 
Since:
MEEP-8.0.0
MEEP 1.9950 (b67)
10-February-2014 08:45

Copyright (c) 2014, Oracle and/or its affiliates. All Rights Reserved. Use of this specification is subject to license terms.