Application Types
Early in your development cycle, you will face an important decision—what kind of application architecture you should use. BlackBerry supports three very different types of programs, and each offers a unique set of advantages and style of development. This section will provide a quick tour and critique of the available options.

MIDlets 
A MIDlet is a Java ME application. Java ME, previously known as J2ME, was developed by Sun Microsystem in the 1990s as an early way to write Java applications for extremely limited devices. The ME stands for Micro Edition, and the initial requirements were very micro indeed: devices could have as little as 192 kilobytes of RAM. Over the years, Java ME has expanded and matured along with the mobile market, gradually adding new features and support as they become widely available in handsets. A collection of optional features for Java ME is called a JSR, or Java Specification Request. You will encounter some of the more popular JSRs later in this book. JSRs cover features like filesystem access, media playback, XML parsing, and more. RIM has been pretty good at adopting the most widespread and useful JSRs. You can find some of a BlackBerry’s supported JSRs by visiting the device’s Options menu, then selecting
About. You will likely see several options such as “Micro Edition File” and “Micro Edition Bluetooth.”


Java ME is available on a wide range of handsets, not just BlackBerry devices. Due to different display sizes, supported JSRs, and other discrepancies, MIDlets rarely actually offer “Write once, run everywhere” functions. Still, porting between two Java ME phones is much easier than porting between two different platforms.


MIDlet Behavior
When a user launches a MIDlet, the device will run it in a stripped-down version of the Java Virtual Machine. Unlike a regular Java SE application, which is entered through a static main() function and runs until its threads are terminated or it calls System.exit(), a MIDlet is a managed application. A managed application’s methods will be invoked by the managing platform when it needs to respond to something, such as the app pausing or the user selecting a button. This architecture should be familiar to developers of Java servlets and other Java EE applications.


The simplest MIDlets need to reply to only three events: the application starting, pausing, or exiting. An application should handle necessary initialization when starting, release scarce resources when pausing, and perform any remaining cleanup when exiting.


MIDlet UI
MIDlet programming supports several choices for user interface programming. The simplest, but most limited, is using Screen objects. Each Screen instance corresponds to an application screen. A Form is a type of Screen that can contain multiple Item objects. Examples of items include text entry fields, labels, and images. Using screens allows you to very quickly build up an application UI with your desired functions. Unfortunately, the UI is usually quite limited and unattractive. Screens tend to look better on BlackBerry devices than on most other Java ME devices, but they still do not look nearly as nice as other UI frameworks.

When using a Screen, users will interact with your application through Command objects.

Each Command is a specific action the user can take. In an email program, the commands might include choices to compose a new message, save the current message, run a spell checker, or exit the app. You will be notified by the application manager when the user has selected a Command. In BlackBerry apps, commands will display when the user presses the BlackBerry Menu key.

An alternative to Screen is to subclass Canvas. A Canvas allows you to completely control the appearance of your app, down to the level of the pixel. When your app needs to draw, it will be provided with a Graphics context. You can use this to directly draw images, text, rectangles, arcs, and even arbitrary blocks of pixels. This system offers maximum flexibility for creating great looking apps. However, it is also considerably more complex.

A Canvas-based app can continue to use Command objects, but it also gains the ability to directly interact with the user. You will be notified when the user presses a key or interacts with the screen, both for click ball-based BlackBerry devices and touch-based ones. With these capabilities, it becomes possible to write more advanced user interfaces. For example, you could add keyboard shortcuts to your email program or flip between images when the user clicks on them.

Finally, a GameCanvas offers a useful set of behaviors for developers who are writing games or other applications that demand a high degree of interactivity. Screen and Canvas apps are primarily reactive, waiting for notifications and deciding how to respond. GameCanvas allows you to directly query the key states and immediately start painting in response. This gives you maximum control over application speed and responsiveness. Additionally, a GameCanvas offers an offscreen Graphics context that you can progressively draw to before copying it directly to the screen.

Comments (0)

แสดงความคิดเห็น