Class | Description |
---|---|
PowerManager |
This class is a common superclass which contains the methods utilized
by normal applications to retrieve system power states information.
|
PowerStateEvent |
Specialized event object that is used for system power state change notifications.
|
PowerStatePermission |
PowerStatePermission grants calls to
PowerManager.setPowerState method. |
Exception | Description |
---|---|
PowerManagerException |
Thrown when a power management related error is encountered.
|
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.
javax.microedition.event
package.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.
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(); } } } |
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 } } } |
Copyright (c) 2014, Oracle and/or its affiliates. All Rights Reserved. Use of this specification is subject to license terms.