Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 07:32, 22 July 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Checking battery level in Java ME

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleTested with
Devices(s): Nokia E70, Nokia N78, Nokia 701, Nokia Asha 305
Created: vltsoy (27 Nov 2008)
Last edited: hamishwillee (22 Jul 2013)



This code snippet demonstrates how to retrieve the current battery level.

The application retrieves the system property which contains the current battery level (in per cent) and displays it. Note that in Series 40 platform only signed MIDlets under operator domain can retrieve this information.

Source file:

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
public class BatteryLevel extends MIDlet implements CommandListener{
private Display display;
private Form form;
private Command exitCommand;
private Command refreshCommand;
private StringItem level;
* Constructor. Constructs the object and initializes displayables.

public BatteryLevel() {
form = new Form("Battery level.");
exitCommand = new Command("Exit", Command.EXIT, 2);
refreshCommand = new Command("Refresh", Command.OK, 1);
level = new StringItem("", "");
display = Display.getDisplay(this);
* From MIDlet.
* Called when the MIDlet is started.

public void startApp() {
// No implementation required.
* From MIDlet.
* Called to signal the MIDlet to enter the Paused state.

public void pauseApp() {
// No implementation required.
* From MIDlet.
* Called to signal the MIDlet to terminate.
* @param unconditional whether the MIDlet has to be unconditionally
* terminated

public void destroyApp(boolean unconditional) {
// No implementation required
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param cmd the command that was invoked
* @param displayable the displayable where the command was invoked

public void commandAction(Command c, Displayable d) {
if (c == refreshCommand) {
} else if (c == exitCommand) {
* Called to read current battery level.

private void checkBatteryLevel() {
String property = System.getProperty("");
if (property == null) {
level.setText("Unable to retrieve current battery level.\n" +
"It's only available for signed midlets " +
"under operator domain.");
} else {
level.setLabel("Current battery level:");


This code displays the current battery level in per cent.

Supplementary material

The example project and binary files are available for download at and

This page was last modified on 22 July 2013, at 07:32.
149 page views in the last 30 days.