May 18th, 2009 in java | leave a response
Introdução
Hoje tive uma aula muito interessante aqui na pós sobre o desenvolvimento para aplicativos móveis usando o
JavaME, depois de fazer alguns exercícios resolvi postar aqui um exemplo bem simples que implementamos durante a aula.
Objetivo do Exemplo
Neste exemplo será criado um formulário com dois
combo onde serão armazenados os nomes de algumas cidades, depois de escolher as cidades será calculado a distância entre elas.
O Código Fonte
package hello;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author Anselmo Battisti
*/
public class HelloMIDlet extends MIDlet implements CommandListener {
private boolean midletPaused = false;
private String[] cidades = {"Tupãssi","JS","Cascavel"};
private int[][] distancia;
// interface
private List listInicioTrajeto;
private List listFimTrajeto;
private TextBox tbxDistancia;
// comandos
Command avancar = new Command("Destino",Command.OK,0);
Command calcular = new Command("Distancia,",Command.OK,0);
//
private Command exitCommand;
private Form form;
private StringItem stringItem;
//
/**
* The HelloMIDlet constructor.
*/
public HelloMIDlet()
{
// distancia entre as cidades
distancia = new int[3][3];
distancia[0][0] = 0;
distancia[1][1] = 0;
distancia[2][2] = 0;
distancia[0][1] = 10;
distancia[1][0] = 10;
distancia[1][2] = 30;
distancia[2][1] = 30;
distancia[0][2] = 40;
distancia[2][0] = 40;
}
private void initialize() {
}
public void startMIDlet() {
switchDisplayable(null, getForm());
}
public void resumeMIDlet() {
}
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
// write pre-switch user code here
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
// write post-switch user code here
}
public void commandAction(Command command, Displayable displayable) {
if(command == avancar){
listFimTrajeto.setTitle("Fim do Trajeto");
listFimTrajeto.addCommand(calcular);
this.getDisplay().setCurrent(listFimTrajeto);
} else if (command == calcular){
String dist = Integer.toString(this.getDistancia(listInicioTrajeto.getSelectedIndex(),listFimTrajeto.getSelectedIndex()));
tbxDistancia.setString(dist);
this.getDisplay().setCurrent(tbxDistancia);
}
}
/**
* Returns an initiliazed instance of exitCommand component.
* @return the initialized component instance
*/
public Command getExitCommand() {
if (exitCommand == null) {
// write pre-init user code here
exitCommand = new Command("Exit", Command.EXIT, 0);
// write post-init user code here
}
return exitCommand;
}
public Form getForm() {
if (form == null) {
// write pre-init user code here
form = new Form("Welcome", new Item[] { getStringItem() });
form.addCommand(getExitCommand());
form.setCommandListener(this);
// write post-init user code here
}
return form;
}
public StringItem getStringItem() {
if (stringItem == null) {
// write pre-init user code here
stringItem = new StringItem("Hello", "Hello, World!");
// write post-init user code here
}
return stringItem;
}
public Display getDisplay () {
return Display.getDisplay(this);
}
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
public void startApp()
{
listInicioTrajeto = new List("Origem",List.EXCLUSIVE,cidades,null);
listInicioTrajeto.setCommandListener(this);
listFimTrajeto = new List("Destino",List.EXCLUSIVE,cidades,null);
listFimTrajeto.setCommandListener(this);
// tela inicial da aplicação
listInicioTrajeto.setTitle("Inicio do Trajeto");
listInicioTrajeto.addCommand(avancar);
tbxDistancia = new TextBox("A Distancia eh", "", 30, TextField.ANY);
this.getDisplay().setCurrent(listInicioTrajeto);
}
private int getDistancia(int cidInicial, int cidFinal)
{
return this.distancia[cidInicial][cidFinal];
}
public void pauseApp() {
midletPaused = true;
}
public void destroyApp(boolean unconditional) {
}
}
Conclusão
Depois de compilar deve aparecer algo +- como a imagem abaixo.

Se você quiser fazer teu aplicativo rodar no teu celuar ai vai algumas dicas:
- Antes de fazer o deploy da aplicação verifique qual versão do MIDP o celular suporta, eu apanhei um pouco nisso pq o meu a1200i roda MIDP 2.0 e eu estava fazendo o deplou para MIDP 2.1
- Não existe uma compatibilidade de interface entre as várias implementações de MIDP ou seja, se vc testou no a1200i não significa que a interface vai se comportaar da mesma forma em outro aparelho, principalmente se ele for de outro fabricante.
May 20th, 2009 at 10:17 am
Reveja no artigo as versões do MIDP que você citou, são as mesmas. Não seriam versões diferentes do MIDP?
May 20th, 2009 at 4:41 pm
se tem razão
valeu pela dica