Recall that BlackBerry applications that support user interaction extend net.rim.device.api.ui.UiApplication
. This class provides methods for applications to register event listeners, manage threads, and manage UI components. When an application starts, the JVM invokes main()
, which in turn calls enterEventDispatcher()
to start handling events.
To get started developing BlackBerry applications with the JDE, you create a workspace, and subdirectories for each project. Then you create source files, build the application, and test it, much as in any IDE. Let's walk through development of a simple example.
- In the IDE, from the File menu, choose New workspace.
- Enter a name for the workspace and a pathname for the directory where it should be saved, and press OK.
- From the Project menu, choose Create new project.
- Enter a name for the project and a directory where it should be saved, and press OK.
|
- From the File menu, choose New.
- Give a name to the file; include the
.java
extension. - Enter the pathname of the project directory where you want the file saved, and press OK.
- In the editor pane, right-click the file, and choose Insert into project.
- Select the project and press OK.
- In the editor pane, enter the code for this sample Blackberry application:
Code Sample 1:HelloApp.java
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
public class HelloApp extends UiApplication {
public static void main(String argv[]) {
HelloApp app = new HelloApp();
app.enterEventDispatcher();
}
public HelloApp() {
pushScreen(new HelloScreen());
}
}
class HelloScreen extends MainScreen {
public HelloScreen() {
super();
LabelField title = new LabelField
("BlackBerry App", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
add(new RichTextField("Welcome to Developing BlackBerry Apps Tutorial"));
}
public boolean onClose() {
Dialog.alert("Visit Again!");
System.exit(0);
return true;
}
}
Once you've entered this code, you can build the project. To compile the source file, perform preverification, and package the application into a .cod
file, from the Build menu choose Build all.
The JDE will generate FirstApp.jad
, FirstApp.jar
, and FirstApp.cod
, among other files, in the project directory you created earlier.
Now you can run the application in the simulator. In the JDE's Build
menu choose Build all and run
. The application will appear in the simulator. Just select it, and it will display a welcome message. When you exit, you'll see the message "Visit Again!" as in Figure 8.
|
To load the application into a real device, use the javaloader
command-line tool as I described earlier:
javaloader -usb load FirstApp.cod
No comments:
Post a Comment