How to get Nokia Battery Level in JavaME

November 9th, 2008 Posted in J2ME / JavaME, Java, Nokia, Tips, Tricks, Guide

 

Nokia Symbian 3rd Edition phone introduces “com.nokia.mid.batterylevel” properties which you can get the current battery charge level by calling System.getProperty(“com.nokia.mid.batterylevel”) from within your midlet application. Its up to your imagination for you to do the rest.

Sample source code (displaying battery charge level on gauge):

/**********
/*
/* Nokia Battery Level midlet
/* Copyright 2008 Mohammad Hafiz bin Ismail ([email protected])
/* http://mobilepit.com
/*
/* May be use freely
**********/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class NokiaBattery extends MIDlet implements CommandListener {

int value;
Display display;
Form form;
Gauge gauge;
Command cmdExit;

public NokiaBattery() {
 display = Display.getDisplay(this);
 form = new Form("Nokia Battery");
 cmdExit = new Command("Exit",Command.EXIT,1);
 form.addCommand(cmdExit);
 value=Integer.parseInt(System.getProperty("com.nokia.mid.batterylevel"));
 gauge = new Gauge("Level",false,100,value);
 form.append(gauge);
form.append("Value :"+value);

 StringItem item = new StringItem("Network Signal",System.getProperty("com.nokia.mid.networksignal"));
 form.append(item);
 form.setCommandListener(this);
}

public void startApp(){
      display.setCurrent(form);
}

public void pauseApp() {}

public void destroyApp(boolean flags) {

	notifyDestroyed();
} 

public void commandAction (Command cmd, Displayable disp)
{
	if (cmd == cmdExit) {
	 	destroyApp(false);
	}
}

}

Download source code and sample midlet : NokiaBattery_midlet.zip

 

Possibly Related Posts...


One Response to “How to get Nokia Battery Level in JavaME”

  1. Horizon Says:

    Thanks for the code man! It is helpful for my j2me developer friend.



Leave a Reply

This blog is protected by dr Dave\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'s Spam Karma 2: 145328 Spams eaten and counting...