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
November 9th, 2008 at 1:13 pm
Thanks for the code man! It is helpful for my j2me developer friend.