Checking battery level in Java ME
(New page: __NOTOC__ __NOEDITSECTION__ {{CodeSnippet |id= |platform=S60 3rd Edition, FP2 |devices=Nokia E70, Nokia N78 |category=Java ME |subcategory=Hardware |creationdate=October 30, 2008 |ke...) |
|||
| Line 6: | Line 6: | ||
|devices=Nokia E70, Nokia N78 | |devices=Nokia E70, Nokia N78 | ||
|category=Java ME | |category=Java ME | ||
| − | |subcategory= | + | |subcategory=System information |
|creationdate=October 30, 2008 | |creationdate=October 30, 2008 | ||
| − | |keywords=java.lang.System.getProperty | + | |keywords=java.lang.System.getProperty, com.nokia.mid.batterylevel |
}} | }} | ||
| + | |||
==Overview== | ==Overview== | ||
| − | |||
| − | + | This code snippet demonstrates how to retrieve current battery level. | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | ==Source file: | + | Application retrieves system property <tt>com.nokia.mid.batterylevel</tt>, |
| + | which contains current battery level in percentage, and displays it. | ||
| + | |||
| + | ==Source file: BatteryLevel.java== | ||
<code java> | <code java> | ||
| − | + | ||
| − | + | ||
import javax.microedition.lcdui.Command; | import javax.microedition.lcdui.Command; | ||
import javax.microedition.lcdui.CommandListener; | import javax.microedition.lcdui.CommandListener; | ||
| Line 30: | Line 28: | ||
import javax.microedition.lcdui.Displayable; | import javax.microedition.lcdui.Displayable; | ||
import javax.microedition.lcdui.Form; | import javax.microedition.lcdui.Form; | ||
| + | import javax.microedition.lcdui.StringItem; | ||
import javax.microedition.midlet.MIDlet; | import javax.microedition.midlet.MIDlet; | ||
| − | + | ||
| − | + | public class BatteryLevel extends MIDlet implements CommandListener{ | |
| − | + | ||
| − | + | ||
| − | public class | + | |
private Display display; | private Display display; | ||
| − | + | private Form form; | |
| − | + | private Command exitCommand; | |
| − | + | private Command refreshCommand; | |
| − | private Form | + | private StringItem level; |
| − | + | ||
| − | + | ||
| − | + | ||
| − | private Command | + | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
/** | /** | ||
* Constructor. Constructs the object and initializes displayables. | * Constructor. Constructs the object and initializes displayables. | ||
*/ | */ | ||
| − | public | + | public BatteryLevel() { |
| − | + | form = new Form("Battery level."); | |
| − | + | ||
| − | + | exitCommand = new Command("Exit", Command.EXIT, 2); | |
| − | + | refreshCommand = new Command("Refresh", Command.OK, 1); | |
| − | + | ||
| − | + | level = new StringItem("Current battery level:", ""); | |
| − | display = Display.getDisplay( this ); | + | form.append(level); |
| − | + | ||
| − | + | form.addCommand(exitCommand); | |
| + | form.addCommand(refreshCommand); | ||
| + | form.setCommandListener(this); | ||
| + | |||
| + | display = Display.getDisplay(this); | ||
| + | display.setCurrent(form); | ||
| − | + | checkBatteryLevel(); | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
} | } | ||
| − | + | ||
/** | /** | ||
* From MIDlet. | * From MIDlet. | ||
| − | * Called when MIDlet is started. | + | * Called when the MIDlet is started. |
| − | + | ||
*/ | */ | ||
| − | public void startApp() | + | public void startApp() { |
| − | + | // No implementation required. | |
} | } | ||
| + | |||
/** | /** | ||
* From MIDlet. | * From MIDlet. | ||
| Line 84: | Line 75: | ||
*/ | */ | ||
public void pauseApp() { | public void pauseApp() { | ||
| − | //No implementation required | + | // No implementation required. |
} | } | ||
| + | |||
/** | /** | ||
* From MIDlet. | * From MIDlet. | ||
| Line 91: | Line 83: | ||
* @param unconditional whether the MIDlet has to be unconditionally | * @param unconditional whether the MIDlet has to be unconditionally | ||
* terminated | * terminated | ||
| − | |||
*/ | */ | ||
| − | public void destroyApp(boolean unconditional) | + | public void destroyApp(boolean unconditional) { |
| − | + | // No implementation required | |
| − | + | ||
} | } | ||
| + | |||
/** | /** | ||
* From CommandListener. | * From CommandListener. | ||
* Called by the system to indicate that a command has been invoked on a | * Called by the system to indicate that a command has been invoked on a | ||
* particular displayable. | * particular displayable. | ||
| − | * @param | + | * @param cmd the command that was invoked |
* @param displayable the displayable where the command was invoked | * @param displayable the displayable where the command was invoked | ||
*/ | */ | ||
| − | public void commandAction( Command | + | public void commandAction(Command c, Displayable d) { |
| − | + | if (c == refreshCommand) { | |
| − | if( | + | checkBatteryLevel(); |
| − | + | } else if (c == exitCommand) { | |
| − | } | + | notifyDestroyed(); |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
} | } | ||
} | } | ||
| + | |||
/** | /** | ||
| − | * | + | * Called to read current battery level. |
*/ | */ | ||
| − | + | private void checkBatteryLevel() { | |
| − | + | String property = System.getProperty("com.nokia.mid.batterylevel"); | |
| − | + | level.setText("\n"+property+"%"); | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
} | } | ||
} | } | ||
| − | |||
| − | + | </code> | |
| − | + | ==Postconditions== | |
| − | + | This code will display current battery level in percentage. | |
| − | + | ==Supplementary material== | |
| − | + | You can see source file and executable application in attached zip archive. Archive is available for download at [[Media:Checking_battery_level_in_J2ME.zip]] | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | You can see source file and executable application in attached zip archive. Archive is available for download at [[Media: | + | |
[[Category:Java ME]][[Category:Code Examples]] | [[Category:Java ME]][[Category:Code Examples]] | ||
Revision as of 17:59, 27 November 2008
Article Metadata
Tested with
Devices(s): Nokia E70, Nokia N78
Compatibility
Platform(s): S60 3rd Edition, FP2
Article
Keywords: java.lang.System.getProperty, com.nokia.mid.batterylevel
Created: (30 Oct 2008)
Last edited: vltsoy
(27 Nov 2008)
Overview
This code snippet demonstrates how to retrieve current battery level.
Application retrieves system property com.nokia.mid.batterylevel, which contains current battery level in percentage, and displays it.
Source file: BatteryLevel.java
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("Current battery level:", "");
form.append(level);
form.addCommand(exitCommand);
form.addCommand(refreshCommand);
form.setCommandListener(this);
display = Display.getDisplay(this);
display.setCurrent(form);
checkBatteryLevel();
}
/**
* 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) {
checkBatteryLevel();
} else if (c == exitCommand) {
notifyDestroyed();
}
}
/**
* Called to read current battery level.
*/
private void checkBatteryLevel() {
String property = System.getProperty("com.nokia.mid.batterylevel");
level.setText("\n"+property+"%");
}
}
Postconditions
This code will display current battery level in percentage.
Supplementary material
You can see source file and executable application in attached zip archive. Archive is available for download at Media:Checking_battery_level_in_J2ME.zip

