Creating an Application with the BlackBerry JDE BLACKBERRY

The BlackBerry JDE is a fully integrated development environment that contains all the tools you need to develop, build, and test your application, on both a simulator and a device. Its editor, while quite usable, is not quite state-of-the-art, and if you’re used to something like Visual Studio or Eclipse, you may find it a step backward. However, it makes up for this by supporting the widest range of features for BlackBerry development; there are times when only the JDE will do. And a lot of developers like it for its integrated nature and straightforward design and usage; you don’t have to play with a lot of settings that might get in the way of developing your application. If you don’t have the JDE installed.

Creating the Workspace and Project
The BlackBerry JDE can have a single workspace loaded at any time. This workspace can contain any number of projects. Each project represents either a BlackBerry application, an alternate entry point to an application, a library, or a MIDlet. We’ll discuss these all in the next chapter; for now, we’ll just build a simple application. Start by launching the JDE. If this is the first time you’ve launched it, you’ll see the samples.jdw workspace already loaded. This contains an array of very useful sample applications from BlackBerry that show you how to use different parts of the BlackBerry API, and it’s a great reference for application developers. For now, we’ll create our own workspace by selecting New Workspace from the File menu. Call the workspace HelloWorld, and select a directory to save it in, as shown.

The JDE Create Workspace dialog

The JDE Create Workspace dialog

The JDE will create a file called HelloWorld.jdw in the directory that you specified Next, create a BlackBerry application project in this workspace by right-clicking HelloWorld.jdw in the JDE and selecting “Create new Project in HelloWorld.jdw” from the pop-up menu, as shown in this.

Creating a new project in the JDE workspace

Creating a new project in the JDE workspace

We’ll call this project HelloWorld as well, and leave the project in the same directory as the workspace, so just type HelloWorld in the “Project name” field as shown in this, and click OK.

Creating the new project within the workspace

Creating the new project within the workspace

There will now be a HelloWorld.jdp file in your directory. By default, this is already a BlackBerry application project so we don’t have to do anything else.

Creating the Application Classes

Now, we’ll create the Java classes that our Hello World application needs in much the same way as creating a project—right click the HelloWorld project, and select “Create new File in Project” as shown in this.

Creating a new class in the JDE

Creating a new class in the JDE

Call the new file HelloWorldApp (you can leave off the .java extension, as it will be added if you don’t explicitly enter another file extension). It’s also a very good idea toput your classes into packages. In the JDE, you can do this by specifying a subdirectory for the new class; the JDE will automatically map that to a package. We’ll use com.beginningblackberry. helloworld as our package name, which maps to the directory com beginning blackberry helloworld. Add that to the end of the directory in the dialog box shown in this, and click OK.

Creating the main HelloWorldApp class in the JDE

Creating the main HelloWorldApp class in the JDE

Now, the HelloWorldApp.java file will be created and opened by default in our workspace. The JDE helpfully fills in a basic class structure for us too, as shown in this.

HelloWorldApp.java

HelloWorldApp.java

We’ll create one more class for our main screen and then start filling in details. Follow the same steps as before, and call the class HelloWorldMainScreen.Or, as a shortcut, you can right click the com/beginningblackberry/helloworld directory instead of the project name to have the JDE automatically fill in the package directory for you. HelloWorldApp and HelloWorldMainScreen are the only classes we’ll need for this application. The application project, as it appears in the JDE, is shown in this.

Both classes for Hello World in the JDE

Both classes for Hello World in the JDE

The Main Application Class

The main application class will need to do three things:

  • Create an instance of the application
  • Create the main screen and push it onto the display stack
  • Start the event dispatch thread

This is generally the pattern you’ll follow for all your applications, unless you need to do something like automatically start when the BlackBerry device boots.

UiApplication

All applications that display a user interface (screens, menus, etc.) must subclass net. rim. device. api. ui. UiApplication. We’ll do this by adding an import to the top of Hello World App. java and making an appropriate change to the class definition (I’ve removed some of the automatically generated comments from the source to keep things shorter):

NOTE: We could have used import net.rim.device.api.ui.* to get the same result, but we imported the specific class instead. When you need to import a class, it’s better to import that class specifically instead of the class’s package. Doing so will improve application performance, and performance is very important on mobile devices. Next, we’ll fill in the constructor of HelloWorldApp. This will create the main screen and push it onto the display stack:

Finally, we’ll need a main method. This will be familiar to you if you’re a Java SE, .NET, or C developer but is different from the Java ME/MIDP way of doing things. The main method acts as the entry point for our application and always has the same signature. You should only have one main method per application. The main method will create an instance of our application, and start the event dispatcher, which is the mechanism that does all the drawing to screen, and listens for all user interaction for our application.

The enterEventDispatcher method will never return as long as the application is running. Essentially, the thread that entered the main application becomes the event dispatch thread. We’ll explore this in greater depth later, but for now, just remember that the method won’t return during the application’s normal life cycle.

Coding the Main Screen Class

If you tried to build the application at this point, you’d get a compile error, because we fudged something. The pushScreen call in HelloWorldApp’s constructor requires a Screen object, which is a subclass of net.rim.device.api.ui.Screen, and HelloWorldMainScreen doesn’t subclass that yet. Let’s fix that now:

We subclass MainScreen instead of Screen, because MainScreen gives us a couple of things automatically, namely a basic layout manager (to position our UI controls on the screen) and a default menu. Later, we’ll want to handle some of that functionality ourselves, but for this application, the default behavior of MainScreen is just what we want. You could run the application now, but you’d just get a blank screen (albeit with a menu containing the Close item). That’s because we haven’t added anything to our main screen yet. Let’s do that and then build and run the application.

Adding Basic Fields

We’ll deal with the user interface more in-depth later, but for now, here’s a brief overview of how things work, so you’re not walking totally blindfolded. The BlackBerry User Interface API follows a Fields/Layout Managers/Screens model: Fields (the user interface controls like buttons and text boxes) are contained within layout managers, which arrange and draw them in specific positions. The managers themselves are contained within other managers, and ultimately a Screen class, which represents the visible display on the BlackBerry. If you’ve used Java’s Abstract Window Toolkit (AWT), Swing, Windows Forms, or any number of other UI toolkits, these concepts will be familiar to you. In fact if you’re an experienced Swing user, you’ll find

things very familiar. For now, I’ll gloss over some of the details, but basically, a MainScreen instance contains a single VerticalFieldManager instance, which arranges all fields that it contains, one below the other, in the order that they’re added.The BlackBerry API contains a useful variety of fields and managers already. For Hello World, we’ll just need one—the LabelField, which displays (as you might expect) a text label. If you’re interested in exploring a bit more, you can find most of the built-in fields in the net. rim. device. api. ui. component package and the built-in layout managers in net. rim. device. api. ui. container. It’s easier to show than explain, so here’s what HelloWorldMainScreen ooks like with the LabelField added:

Now, the application’s finished! Let’s take a look at it in action in the simulator.

Running the Simulator

Running an application in the simulator is very easy. Remember when we talked about how easy setup is with the JDE? From the Debug menu, click Go—that’s it.The JDE will automatically build your project (hopefully, you don’t have any build errors) and deploy it to the simulator, and the default simulator for your version of the JDE will start. We used the JDE version 4.2.1, which has the BlackBerry 8800 as its default simulator, so we get a window that looks like this the one shown in this.

The default JDE version 4.2.1 simulator is the BlackBerry 8800.

The default JDE version 4.2.1 simulator is the BlackBerry 8800.

Your application won’t be started by default, so you’ll have to navigate to it and start it. There are two things to remember with the simulator:

  • Unless you’re using the JDE version 4.7 with the BlackBerry Storm simulator, you can’t click the screen—well, you can, but it won’t do anything, as most BlackBerry devices don’t have touch screens.
  • The trackball registers up, down, left, and right movements; it basically like the arrow keys on your keyboard. In fact, the easiest way to operate the trackball in the simulator is to use the arrow keys on your PC’s keyboard.

For most devices, to run the application in the simulator, you can follow these instructions:

  1. Navigate down to the Applications icon and press Enter on your keyboard.
  2. The Applications icon on the simulator

    The Applications icon on the simulator

  3. Navigate to the HelloWorld icon, and press Enter.
  4. The HelloWorld Icon on the simulator

    The HelloWorld Icon on the simulator

  5. The application will start: HelloWorldMainScreen will display our LabelField, which says Hello World as shown in this.
  6. Hello world!

    Hello world!

This application is not the most exciting, certainly, but it’s a real, fully functional application. It has a menu with a Close menu item (you can open the menu by clicking the BlackBerry key to the left of the trackball), and you can start and exit it, and with a tiny bit of work, you’ll be able to put it onto a real device. The first thing we’ll need to do is build and sign our application.

Building and Signing Your Application

Building on the JDE is as simple as clicking Build from the Build menu. The Hello World application is simple enough that you don’t need to sign it to run on a device, but it’s a good idea to get used to signing your applications for when you will need it. If you don’t have your code signing keys yet, you can skip the rest of this section. After you’ve built your application, select Request Signatures from the Build menu. Notice that all signatures are listed as optional. We don’t need to sign, but it won’t hurt, so click the Request button, and enter your password. You’ll see a progress dialog and, if all goes well, messages letting you know that your application has been signed.

Requesting signatures for Hello World

Requesting signatures for Hello World

Building with Different JDE Versions

If you want to build with different versions of the JDE, you’ll have to reload your workspace under each version of the JDE you want to use. Generally, building with the earliest version that supports all the features that you need is a good idea. This gives your application compatibility with the widest range of BlackBerry devices. You may need to build and run under a later version if you want to test with a simulator (such as the touch screen BlackBerry Storm) that’s only available for certain versions of the JDE. Also, if you want to use a specific API, you’ll obviously need to use the appropriate version of the JDE; JDE versions that APIs appear in are usually specified in the BlackBerry API Javadocs.

Loading Hello World onto a Device

You can load a BlackBerry application onto a device in several ways. For now, to avoid discussion of different descriptor files, we’ll use a developer tool called JavaLoader. JavaLoader is a command-line tool, meaning you have to run it from the Windows command prompt. Start a command prompt window, and navigate to your working directory. Then, make sure your BlackBerry is plugged into a USB port on your computer before typing the following:

with the path to the bin directory under your JDE installation, usually C:Program FilesResearch In MotionBlackBerry JDE 4.2.1bin. Your application should load on to your device almost instantly, and you’ll be ready to show off Hello World to your friends!

Using the Debugger

The simulator and JDE debugger are as powerful a combination as you’d expect from any modern development environment. You can simulate different network coverage, battery levels, and other device configurations, as well as events like phone calls through the Simulate menu. You can also set a breakpoint and step through your code at any point. Of course, no simulator is a 100 percent perfect representation of the real thing, so the debugger also lets you connect to your application running on a real BlackBerry device through a USB connection.

Changing Simulators and Settings

Your choice of simulators is limited to those that came with the version of the JDE you’re using and any others you’ve downloaded and installed .To select a simulator other than the default, select Preferences from the Edit menu, and click the Simulator tab.

The Simulator tab in the JDE’s Preferences window

The Simulator tab in the JDE’s Preferences window

On the Simulator tab, you can choose a specific simulator to run and modify quite a few other useful parameters, for example:

  • PIN: This is often used as a unique device ID for a client-server application.
  • Launch Mobile Data Service (MDS) with simulator: This is required

for simulating most BlackBerry networking applications.

Setting Breakpoints

Setting a breakpoint is easy. Just right-click somewhere in your source file, and select Set Breakpoint At Cursor (or press F9 to set a breakpoint at the line the cursor is currently on). A breakpoint will cause your application to pause and open the debug view when that line is reached.From that point, you can step through your code line-byline to look at the values of variables, the call stack, and other information. Let’s try this with Hello World now. Open HelloWorldMainScreen.java in the editor and move the cursor to the first line in the constructor, as shown in this.

Move the cursor to the first line of the HelloWorldMainScreen constructor.

Move the cursor to the first line of the HelloWorldMainScreen constructor.

Right-click, and add a breakpoint. You’ll see a red ball indicating the breakpoint.

A breakpoint is indicated by the circle to the left of the LabelField initialization.

A breakpoint is indicated by the circle to the left of the LabelField initialization.

Now, start the simulator if it isn’t still running, and start the application. Since this breakpoint is in a constructor, the debugger should come up right away (see Figure). The JDE window will have a few new panels available, including the Calling Method panel. It displays that call stack and shows that we’re in the HelloWorldMainScreen constructor, which was called from the HelloWorldApp constructor, which was called from the main method in HelloWorldApp.

Execution stopped at a breakpoint with the JDE. The call stack is visible in the middle-left panel and the variables in the bottom-right one.

Execution stopped at a breakpoint with the JDE. The call stack is visible in the middle-left panel and the variables in the bottom-right one.

In the bottom-right panel is a list of variables that are currently in scope.If we step to the next line (using F10 or Debug ➤ Step Over), labelField will appear in that list. Additional information and functionality, such as profiling, is available from the View menu.

Debugging on a BlackBerry Device

Debugging on a device lets you test things that are difficult or impossible with the simulator like specific network conditions and memory and speed constraints. The debugger lets you set breakpoints that will cause the device to suspend execution, as well as allowing you to see the output of System.out.println statements in your application. To debug your application on a device, you must have your device connected to your computer via USB cable. Also, the BlackBerry Desktop Manager should not be running; it will cause connection problems with the debugger.With your BlackBerry device plugged in and the application loaded (but not running), click the Debug menu, and select Attach To. Your device’s PIN should appear in the menu as in Figure. Click it, and after a few seconds for the debugger to attach (messages will appear on the device’s screen), you’ll be able to do everything you can with a simulator.

Attaching the debugger to a real device

Attaching the debugger to a real device

Now, if you’ve kept the same breakpoint from the “Setting Breakpoints” section, when you launch the application the JDE will pop up in the same view with an arrow pointing to the breakpoint at the same line.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

BLACKBERRY Topics