User loginSearchNavigationActive forum topics |
Simple J2ME MIDlet Example
The MIDlet Base ClassThis is the basic shell for all MIDlet applications. You can't make a smaller MIDlet than this. You always need to start with the MIDlet base class so the first import is required. A constructor is required for every class, here it's called Hello(). The other three methods are MIDlet abstract methods required by all MIDlet objects. import javax.microedition.midlet.*;
public class Hello extends MIDlet
{
public Hello() {}
public void pauseApp() {}
public void destroyApp(boolean ignore) {}
public void startApp() {}
}Hello World MIDletThat's the basic shell. Let's do something with it. This is the shortest J2ME program I've written. It's basically one line, although the lcdui import is also required since we want to display something on the phone. The program itself is just nested function calls. Display.getDisplay() is a static method which returns a handle to the phone's display. The setCurrent() method allows you to change the contents of the display. This example changes the display to a new TextBox object with the given title and contents. The last two parameters of the TextBox constructor are the maximum size and optional input constraints (in this case none). import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Hello extends MIDlet
{
public Hello() {}
public void pauseApp() {}
public void destroyApp(boolean ignore) {}
public void startApp()
{
Display.getDisplay(this).setCurrent(
new TextBox("Hello", "The End", 9, 0));
}
}Compile, Package, DeployIf you haven't done it already, look through the download section of SprintDevelopers and install Sun's JRE and Sprint's WTK. For more information, read the How to Get Started article. Using KToolbar, you'll create a new project called "Hello". That will create a apps/Hello directory structure for you including an apps/Hello/src directory where you can put the Hello.java file. From the KToolbar, open the new Hello project and click the "Run" button. A phone should appear with the new Hello program as the only item on the menu. Select the program and you'll see a "Hello" screen with a text box that says "The End". Now that we know the program works, let's put it on an actual phone. From KToolbar, look under the Project menu and select the "Package" option. KToolbar should report that two files were created like this: Building "Hello" Wrote c://SprintPcsWTK/apps/Hello/bin/Hello.jar Wrote c://SprintPcsWTK/apps/Hello/bin/Hello.jad Build complete Now head over to Sprint Developers and use the Uploader. Put in your phone number and the JAR file. You don't need the JAD. You should get a text message on your phone with a link to the Hello MIDlet. Follow the link, acknowlege that this isn't a trusted Sprint file but you've found forgiveness in your heart and you'll use it anyway. Once the program has been installed, you'll have to launch it from your phone. I think this varies from phone to phone, but my T608 immediately gives me the option of running new downloads. I don't have to go hunting for them. Don't Expect MuchThe Hello program is as basic as it gets. You'll notice it doesn't even have an "Exit" option. You'll have to "manually" break out of it. I press the option key on my phone and choose "Exit" to end the program. All phones should have a similar option but if all else fails, turn your phone off. The JAR size comes out to 869 bytes for me. Seems like a lot for basically an empty program, doesn't it? Can anybody make it smaller? I guess you could make all the names shorter and eliminate some of the text. You could also try an obfuscator. Larger ExampleIf you'd like to see a more complicated MIDlet example that actually does something useful, look at the J2ME Example of Scraping Web Pages
|