Java Tutorial/J2ME/MIDlet — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 04:58, 1 июня 2010
Содержание
A first MIDlet with simple text and a few commands
/* License
*
* Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
// A first MIDlet with simple text and a few commands.
public class SimpleTextCommandMIDlet extends MIDlet implements CommandListener {
// The exit, info, and buy commands
private Command exitCommand;
private Command infoCommand;
private Command buyCommand;
// The display for this MIDlet
private Display display;
public SimpleTextCommandMIDlet() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.SCREEN, 1);
infoCommand = new Command("Info", Command.SCREEN, 2);
buyCommand = new Command("Buy", Command.SCREEN, 2);
}
// Start the MIDlet by creating the TextBox and
// associating the exit command and listener.
public void startApp() {
TextBox t = new TextBox("FirstMIDlet", "Welcome to MIDP Programming", 256,
0);
t.addCommand(exitCommand);
t.addCommand(infoCommand);
t.addCommand(buyCommand);
t.setCommandListener(this);
display.setCurrent(t);
}
// Pause is a no-op because there are no background
// activities or record stores to be closed.
public void pauseApp() {
}
// Destroy must cleanup everything not handled
// by the garbage collector.
// In this case there is nothing to cleanup.
public void destroyApp(boolean unconditional) {
}
// Respond to commands. Here we are only implementing
// the exit command. In the exit command, cleanup and
// notify that the MIDlet has been destroyed.
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
}
detect and report MIDlet run-time environment
/* License
*
* Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
/* imports */
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class J2MEEnvMyMidlet extends MIDlet {
/**
* PropertyOutput - lookup and report one application property.
*/
private void PropertyOutput(String key) {
String value = getAppProperty(key);
if (value != null)
System.out.println(key + " = " + value);
}
/**
* PropertyReport - detect and report MIDlet run-time environment.
*/
private void PropertyReport() {
final int Npush = 10; /* maximum number of Push"s to report */
final int Nmidlet = 10; /* maximum number of MIDlet"s to report */
final String key[] = { /* standard names */
"MIDlet-Name", "MIDlet-Version", "MIDlet-Vendor", "MIDlet-Icon",
"MIDlet-Description", "MIDlet-Info-URL", "MIDlet-Jar-URL",
"MIDlet-Jar-Size", "MIDlet-Data-Size", "MicroEdition-Profile",
"MicroEdition-Configuration", "MIDlet-Permissions",
"MIDlet-Permissions-Opt", "MIDlet-Install-Notify",
"MIDlet-Delete-Notify", "MIDlet-Delete-Confirm", null };
final String push_key = "MIDlet-Push-";
final String midlet_key = "MIDlet-";
int i;
for (i = 0; key[i] != null; ++i)
/* report standard properties */
PropertyOutput(key[i]);
for (i = 1; i <= Npush; ++i)
/* report push entries */
PropertyOutput(push_key + i);
for (i = 1; i <= Nmidlet; ++i)
/* report MIDlet entries */
PropertyOutput(midlet_key + i);
return;
}
/**
* MyMIDlet - constructor
*/
public J2MEEnvMyMidlet() {
PropertyReport();
}
/**
* startApp - MIDlet has entered the Active state.
*/
public void startApp() {
}
/**
* pauseApp - enter the Paused state.
*/
public void pauseApp() {
}
/**
* destroyApp - enter the Destroyed state.
*
* @param unconditional
*/
public void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
if (unconditional == false) {
throw new MIDletStateChangeException();
}
}
}
/*
Running with storage root C:\Documents and Settings\Administrator\j2mewtk\2.5.2\appdb\MediaControlSkin
Running with locale: English_United States.1252
Running in the identified_third_party security domain
No audio device found.
MIDlet-Name = Main Midlet Suite
MIDlet-Version = 1.0.0
MIDlet-Vendor = Midlet Suite Vendor
MIDlet-Jar-URL = Main.jar
MIDlet-Jar-Size = 1691
MicroEdition-Profile = MIDP-2.0
MicroEdition-Configuration = CLDC-1.1
Execution completed.
3399135 bytecodes executed
166 thread switches
1667 classes in the system (including system classes)
17751 dynamic objects allocated (535200 bytes)
2 garbage collections (457948 bytes collected)
*/
extends MIDlet
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
public class J2MEHiSmallWorld extends MIDlet {
private TextBox textbox;
public J2MEHiSmallWorld() {
textbox = new TextBox("", "Hi Small World!", 20, 0);
}
public void startApp() {
Display.getDisplay(this).setCurrent(textbox);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
KVM Properties
public class KVMProperties {
private static final String[] properties = {
"microedition.configuration",
"microedition.encoding",
"microedition.platform",
"microedition.profiles"
};
public static void main(String[] args) {
for (int i = 0; i < properties.length; i++) {
System.out.println(properties[i] + " = " +
System.getProperty(properties[i]));
}
}
}
list Capabilities
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
public class CapabilitiesMIDlet extends MIDlet {
private String[] capabilitiesIdx = { "microedition.media.version", "supports.mixing",
"supports.audio.capture", "supports.video.capture", "supports.recording", "audio.encodings",
"video.encodings", "video.snapshot.encodings", "streamable.contents" };
public void startApp() {
List list = new List("Audio/Video Capabilities", Choice.IMPLICIT);
for (int i = 0; i < capabilitiesIdx.length; ++i) {
list.append(System.getProperty(capabilitiesIdx[i]) + " - " + capabilitiesIdx[i], null);
}
Display.getDisplay(this).setCurrent(list);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
Retrieving the predefined properties
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.midlet.MIDlet;
public class RuntimeAccessMIDlet extends MIDlet {
public void startApp() {
System.out.println("MIDlet-Name: " + getAppProperty("MIDlet-Name"));
System.out.println("MIDlet-Vendor: " + getAppProperty("MIDlet-Vendor"));
System.out.println("MIDlet-Version: " + getAppProperty("MIDlet-Version"));
System.out.println("MIDlet-Description: " + getAppProperty("MIDlet-Description"));
System.out.println("Target-Devices: " + getAppProperty("Target-Devices"));
System.out.println("Display-Size: " + getAppProperty("Display-Size"));
InputStream is = this.getClass().getResourceAsStream("/readme.txt");
try {
if (is != null) {
int character;
while ((character = is.read()) != -1) {
System.out.print((char) character);
}
is.close();
}
} catch (IOException e) {
System.out.println(e);
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
Skeleton of a MIDlet
import javax.microedition.midlet.MIDlet;
public class MyMIDlet extends MIDlet {
public MyMIDlet() {
// constructor
}
public void startApp() {
// entering active state
}
public void pauseApp() {
// entering paused state
}
public void destroyApp() {
// entering destroyed state
}
}