×
Namespaces

Variants
Actions
(Difference between revisions)

Checking battery level in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
seppo_fn (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Add Abstract. Tidy wiki text)
(17 intermediate revisions by 5 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Java ME]][[Category:Code Examples]][[Category:Base/System]][[Category:Device Management]][[Category:Symbian]][[Category:Series 40]][[Category:Java Runtime 2.3 for Symbian]][[Category:Nokia Belle]][[Category:Series 40 Developer Platform 1.1]][[Category:Series 40 Developer Platform 2.0]]
__NOEDITSECTION__
+
{{ArticleMetaData <!-- v1.2 -->
{{CodeSnippet
+
|sourcecode= [[Media:BatteryLevelSource.zip]]
|id=&nbsp;
+
|installfile= [[Media:BatteryLevelBinaries.zip]]
|platform=S60 3rd Edition, S60 3rd Edition, FP1, S60 3rd Edition, FP2
+
|devices= Nokia E70, Nokia N78, Nokia 701, Nokia Asha 305
|devices=Nokia E70, Nokia N78
+
|sdk= [http://www.developer.nokia.com/Develop/Java/Tools/ Nokia SDK 1.1 for Java], [http://www.developer.nokia.com/Develop/Java/Tools/ Nokia SDK 2.0 for Java]
|category=Java ME
+
|platform= Series 40, Symbian
|subcategory=System information
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|creationdate=October 30, 2008
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|keywords=java.lang.System.getProperty, com.nokia.mid.batterylevel
+
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= java.lang.System.getProperty, com.nokia.mid.batterylevel
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20081127
 +
|author= [[User:Vltsoy]]
 +
<!-- The following are not in current metadata -->
 +
|id= CS001201
 
}}
 
}}
 
  
 
==Overview==
 
==Overview==
  
This code snippet demonstrates how to retrieve current battery level.
+
{{Abstract|This code snippet demonstrates how to retrieve the current battery level.}}
  
Application retrieves system property <tt>com.nokia.mid.batterylevel</tt>,
+
The application retrieves the system property {{Icode|com.nokia.mid.batterylevel}} 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.
which contains current battery level in percentage, and displays it.
+
 
+
Note that in Series 40 platform, only signed MIDlets under operator domain can retrieve this information.
+
  
 
==Source file: BatteryLevel.java==
 
==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 33: Line 42:
 
import javax.microedition.midlet.MIDlet;
 
import javax.microedition.midlet.MIDlet;
  
 +
       
 
public class BatteryLevel extends MIDlet implements CommandListener{
 
public class BatteryLevel extends MIDlet implements CommandListener{
  
Line 51: Line 61:
 
         refreshCommand = new Command("Refresh", Command.OK, 1);
 
         refreshCommand = new Command("Refresh", Command.OK, 1);
  
         level = new StringItem("Current battery level:", "");
+
         level = new StringItem("", "");
 
         form.append(level);
 
         form.append(level);
  
Line 110: Line 120:
 
     private void checkBatteryLevel() {
 
     private void checkBatteryLevel() {
 
         String property = System.getProperty("com.nokia.mid.batterylevel");
 
         String property = System.getProperty("com.nokia.mid.batterylevel");
 +
        if (property == null) {
 +
            level.setLabel("Error:\n");
 +
            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:");
 
         level.setText("\n"+property+"%");
 
         level.setText("\n"+property+"%");
 +
        }
 
     }
 
     }
 
}
 
}
 
 
 
</code>
 
</code>
  
 
==Postconditions==
 
==Postconditions==
  
This code will display current battery level in percentage.
+
This code displays the current battery level in per cent.
  
 
==Supplementary material==
 
==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]]
+
The example project and binary files are available for download at [[Media:BatteryLevelSource.zip]] and [[Media:BatteryLevelBinaries.zip]].
 
+
[[Category:Java ME]][[Category:Code Examples]]
+

Revision as of 09:26, 5 October 2012

Article Metadata
Code ExampleTested with
Devices(s): Nokia E70, Nokia N78, Nokia 701, Nokia Asha 305
CompatibilityArticle
Keywords: java.lang.System.getProperty, com.nokia.mid.batterylevel
Created: vltsoy (27 Nov 2008)
Last edited: hamishwillee (05 Oct 2012)

Contents

Overview

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

The application retrieves the system property com.nokia.mid.batterylevel 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: 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("", "");
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");
if (property == null) {
level.setLabel("Error:\n");
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:");
level.setText("\n"+property+"%");
}
}
}

Postconditions

This code displays the current battery level in per cent.

Supplementary material

The example project and binary files are available for download at Media:BatteryLevelSource.zip and Media:BatteryLevelBinaries.zip.

295 page views in the last 30 days.
×