This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Java Micro Edition and the fly likes Getting Battery Level Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Getting Battery Level" Watch "Getting Battery Level" New topic

Getting Battery Level

s rolands

Joined: Jun 16, 2007
Posts: 3
Can anyone see whats wrong with this... It builds ok in WTK 2.5.1 but when loaded to i415 Motorola phone it says "Unhandled Exception"

Just trying to get the battery level from the phone. I KNOW this CAN be done on Motorola i415 but I just cannot seem to figure it out. 3 days is enough searching... please help!

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.location.*;

public class HelloMidp extends MIDlet implements CommandListener {
private Command exitCommand;

Display display;
Form mainForm;

public HelloMidp () {
exitCommand = new Command("Exit", Command.EXIT, 1);
mainForm = new Form ("HelloMidp");
String batt = System.getProperty("batterylevel");
mainForm.append (batt);


protected void startApp()

* This method is called to notify the MIDlet to enter a paused
* state. The MIDlet should use this opportunity to release
* shared resources.
protected void pauseApp() {}

* If the MIDlet was using resources, it should release
* them in this method.
protected void destroyApp(boolean bool) {}

public void commandAction(Command cmd, Displayable disp)
if (cmd == exitCommand) {
[ June 16, 2007: Message edited by: Bear Bibeault ]
Ed Schepis

Joined: Feb 06, 2004
Posts: 25
it seems that in

String batt = System.getProperty("batterylevel");
mainForm.append (batt);

you're appending a null to the form causing an unexpected exception.

Consider that (from Motorola developer support website):

"...some of the system properties may return null as they may not be supported on early devices and some system propoerties require the MIDlet to be trusted otherwise return null..."


SCMAD 1.0 - SCEA - SCJA - [<a href="" target="_blank" rel="nofollow">My Weblog</a>]
s rolands

Joined: Jun 16, 2007
Posts: 3

Thanks for the reply. I think you are right about it being null, but , I DO know for a fact this phone can do this. (I have a application that does it on my phone). I think the problem is I forgot to Install the SDK for my phone. I have done that but it now needs to be added to my WTK somehow? Here's more info :

I have installed WTK, and it has been working but I realized I forgot to add the iden SDK for my motorola phone. I installed the SDK but I assume I need to link the files to WTK somehow?

What I am wanting to do :


The SDK has a class file called StatusManager which can retrieve phone battery level...

From documentation:
"Field Detail

public static final int BATTERY_LEVEL

This is the value passed to the status manager to retrive the battery levels

The return value is in the range of [0 - 3] with 0 being no charge and 3 being a full battery.
BATTERY_LEVEL has a value of 0"

QUESTION IS: Where do I put the SDK files so WTK can use them? I am lost here.
I found the StatusManager.class file in :
C:\Program Files\Motorola\iDEN SDK for J2ME (MIDP 2_0)\lib\i730\com\mot\iden\device\

Can someone give me the detailed process of using the SDK with WTK... please??

Do I use the StatusManager.class file alone with my Midlet... or?
s rolands

Joined: Jun 16, 2007
Posts: 3
Such a simple question, but nobody can answer it? Guess I am just to much of a "newbie".
I agree. Here's the link:
subject: Getting Battery Level
It's not a secret anymore!