Professional Documents
Culture Documents
DSOLFDFLRQHVHQ-0(
9HUVLyQGHOGRFXPHQWR
)HFKDGHOGRFXPHQWRGH0DU]RGH
ËQGLFH
Ë1',&(
,1752'8&&,Ï1
,QVWDODFLyQHLQLFLRGH:LUHOHVV7RRONLW
&UHDUQXHYRSUR\HFWR
'HVDUUROORGHOSURJUDPD
&RPSLODFLyQ\GHSXUDFLyQ
5()(5(1&,$6
$1(;26
3.1. ANEXO A: EJEMPLOS DE APLICACIONES J2ME............................................................................. 6
(MHPSOR9LVRUGHLPiJHQHV
(MHPSOR$ODUPD
(MHPSOR7LPHU
2
,QWURGXFFLyQ
Los pasos a seguir para crear una aplicación Java para nuestro terminal Java-
enabled. Lo primero de todo será conseguir e instalar los entornos de
desarrollo necesarios para empezar a desarrollar:
,QVWDODFLyQHLQLFLRGH:LUHOHVV7RRONLW
- La primera permitirá a aquellos usuarios de Forte for Java crear aplicaciones (MIDlets) desde
dicho entorno, para lo cual deberá estar instalado ya el programa.
- La segunda opción no incluye esta posibilidad y será el usuario el que edite el código fuente
con el programa que desee.
ELQ de la aplicación.
3
&UHDUQXHYRSUR\HFWR
4
directorios a partir del directorio MPHZWDSSV que son:
Como resultado de la creación del nuevo proyecto se crearán una serie de
UHV: Contiene los recursos que se van a utilizar, por ejemplo, imágenes,
código.
de la aplicación.
'HVDUUROORGHOSURJUDPD
&RPSLODFLyQ\GHSXUDFLyQ
5HIHUHQFLDV
x http://wireless.java.sun.com
5
x
x
http://www.java.sun.com
x
http://www.corej2me.com
x
http://www.cswl.com
http://www.billday.com
$QH[RV
$QH[R$(MHPSORVGHDSOLFDFLRQHV-0(
(MHPSOR9LVRUGHLPiJHQHV
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
/*--------------------------------------------------
* Main class definition
*-------------------------------------------------*/
public class pngViewer extends MIDlet implements CommandListener
{
private Display display;
private TextBox tbxMain;
private Form frmViewPng;
private Command cmdExit;
private Command cmdView;
private Command cmdBack;
/*--------------------------------------------------
* Constructor for the class.
*-------------------------------------------------*/
public pngViewer()
6
{
display = Display.getDisplay(this);
// ---------------------------------------
/*--------------------------------------------------
* Called by the Application Manager on the device
* to start the MIDlet.
*-------------------------------------------------*/
public void startApp()
{
// Display the Main textbox
display.setCurrent(tbxMain);
}
/*--------------------------------------------------
* A required method.
* Does nothing in this MIDlet
*-------------------------------------------------*/
public void pauseApp()
{
}
/*--------------------------------------------------
* A required method.
* Does nothing in this MIDlet
7
*-------------------------------------------------*/
public void destroyApp(boolean unconditional)
{
}
/*--------------------------------------------------
* Process events
*-------------------------------------------------*/
public void commandAction(Command c, Displayable s)
{
// If the Command button pressed was "Exit"
if (c == cmdExit) {
destroyApp(false);
notifyDestroyed();
// Get the image from the web and append to the form
Image img;
if ((img = getImage(tbxMain.getString())) != null)
frmViewPng.append(img);
display.setCurrent(tbxMain);
}
}
/*--------------------------------------------------
* Open an http connection and download a png file
* into a byte array.
*-------------------------------------------------*/
private Image getImage(String imageStr)
{
try
{
HttpConnection conn = (HttpConnection) Connector.open(imageStr);
8
if(conn.getResponseCode() == HttpConnection.HTTP_OK )
{
int length = (int) conn.getLength();
if (length > 0)
{
byte imageData[] = new byte[length];
InputStream in = conn.openInputStream();
}
}
else
{
frmViewPng.append("Error getting png");
}
}
catch (IOException e)
{
frmViewPng.append("IO Error");
}
return null;
}
}
9
(MHPSOR$ODUPD
import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Timer;
import java.util.TimerTask;
public alarm()
{
display = Display.getDisplay(this);
10
// Add to form and listen for events
dateIndex = frmMain.append(dtfalarmTime);
frmMain.addCommand(cmdalarm);
frmMain.addCommand(cmdReset);
frmMain.addCommand(cmdExit);
frmMain.setCommandListener(this);
frmMain.setItemStateListener(this);
}
11
timerMaster = new Timer();
alarmTimer timerSlave = new alarmTimer();
12
De la ejecución de esta aplicación se obtiene una serie de pantallas como
estas:
13
(MHPSOR7LPHU
import java.lang.*;
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
/**
* A simple class that shows an example of using a Timer and
* a TimerTask.
*
* This MIDlet creates two gauges. One gauge gaugeOne,
* sets elements from low to high. The other, gaugeTwo,
* set elements from high to low. In effect, this has
* gaugeOne "going up", and gaugeTwo "going down."
*
* The two timers fire at different intervals.
*
* There are two commands on our form:
*
* OK: toggles whether the times are active or not.
* EXIT: exits the MIDlet.
*/
14
private Command cmdOK; // OK command
private Command cmdExit; // EXIT command
/**
* Internal class that provides a TimerTask.
*/
private class MyTimerTask extends TimerTask {
private Gauge myGauge; // reference to gauge
private boolean goUp; // if true, go up
private int num; // number of times called
/**
* Public constructor: stores "direction" and a reference to
* a gauge.
*/
public MyTimerTask(Gauge g, boolean up) {
myGauge = g;
goUp = up;
}
/**
* As the timer fires, this method is invoked. Set gauge
* based on goUp
*/
public void run() {
num++;
myGauge.setValue(goUp ?
GAUGE_MAX -(num % GAUGE_MAX) :
num % GAUGE_MAX);
}
}
/**
* Public constructor: gets handle to display,
* creates form, gauges, and commands.
*/
public TimerMIDlet() {
myDisplay = Display.getDisplay(this);
myScreen = new Form("TimerMIDlet");
gaugeOne = new Gauge("Up Gauge",
false,
GAUGE_MAX,
0);
myScreen.append(gaugeOne);
15
false,
GAUGE_MAX,
GAUGE_MAX);
myScreen.append(gaugeTwo);
/**
* Changes the state of timers to/from active to/from
* not-active.
*/
private void flipFlop() {
if (timersRunning) {
timerTaskOne.cancel();
timerTaskTwo.cancel();
timer.cancel();
timersRunning = false;
} else {
timer = new Timer();
timerTaskOne = new MyTimerTask(gaugeOne, false);
timerTaskTwo = new MyTimerTask(gaugeTwo, true);
timer.schedule(timerTaskOne, 0, 1000);
timer.schedule(timerTaskTwo, 0, 1500);
timersRunning = true;
}
}
/**
* Called by the system to start our MIDlet.
* @exception MIDletStateChangeException
*/
protected void startApp() throws MIDletStateChangeException {
myDisplay.setCurrent(myScreen);
flipFlop();
}
/**
* Called by the system to pause our MIDlet.
* No actions required by our MIDLet.
*/
protected void pauseApp() {}
/**
* Called by the system to end our MIDlet.
16
* No actions required by our MIDLet.
*/
protected void destroyApp(boolean unconditional) {}
/***
* Respond to command selections. Process two commands:
*
* OK: flip flop the timers to/from active
* EXIT: exit this MIDlet
*
*/
public void commandAction(Command c, Displayable d) {
if (c == cmdOK) {
flipFlop();
} else if (c == cmdExit) {
destroyApp(false);
notifyDestroyed();
}
}
}
17