Date Created: Fri 21-Jan-2011

Related Document Categories:

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/



How to create a simple BlackBerry application using the BlackBerry Developer Plugin for Eclipse

https://www.blackberry.com/bdsc/?lang=na#register

Logged in and registered for Blackberry Developer plugin for Eclipse

The downloaded plugin is compatible only for Eclipse 3.5 (Galileo)







Once the installation is complete, launch Eclipse and create a new workspace for your Blackberry projects




Once the workspace has loaded create a new BlackBerry project

File-New-Other







When the project loads you will be presented with a BlackBerry getting started page. In the future you can open the Getting Started page selecting Open BlackBerry Getting Started page from the help menu.




I have chosen to look at the tutorial called HelloWorld to get a quick feel of the development and deployment process I have added a few changes to the BlackBerry Helloworld app,but it is essentially based on the Hello world sample code.


The following shows how to create a BlackBerry┬« device application that displays the text ‘Hello World!’ on the BlackBerry device screen. When a BlackBerry device user selects the Close menu item from the menu of the Hello World application , the Hello World application displays a dialog box with the text ‘Goodbye!’.


Create a project for the Hello World application
  • On the File menu, click New > Project.
  • Expand the BlackBerry folder.
  • Select BlackBerry project.
  • Click Next.
  • In the Project name field, type SampleApp.
  • Click Next.
  • Click Finish.
  • Specify the Application type and properties for the SampleApp application project
  • In the Project Explorer view, expand a BlackBerry project.
  • Double-click the BlackBerry_App_Descriptor.xml file.
  • Click the Application tab.
  • In the Title field, type SampleApp Sample.
  • In the Version field, type a version number.
  • In the Vendor field, type a vendor name, for example, Steve Robinson
  • In the Application type drop-down list, select BlackBerry Application.
  • Click Close.
  • Click Yes.






Add Java source files to the SampleApp project

The Java source file name must match the name of the public class or interface in the file. It must also be located in a folder structure that matches its package name. For example, if the package name is com.Mycompany.MyClass, you must name the .java file MyClass.java, and store it in C:\project\com\mycompany\MyClass.java.
  • In the Package Explorer view, right-click the SampleApp project. Click New > Class.
  • In the Name field, type SampleApp.
  • In the Package field, type com.screv.
  • Next to the SuperClass field, click Browse.
  • In the Choose a type field, type UI.
  • Click UIApplication.
  • Click OK.
  • In the Which method stubs would you like to create section, select the following stubs:
  • public static void main (String[] args)
  • Constructors from superclass
  • Inherited abstract methods
  • Click Finish.






Click Browse to implement the UIApplication interface.Type UIApplication in the Choose a type textbox and a list of matching BlackBerry classes will be displayed.








Click Finish to see the following class created


package com.screv;

import net.rim.device.api.ui.UiApplication;

public class SampleApp extends UiApplication {

public SampleApp() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}

}

Since our application needs some extra java classes we are going to use some import statements


Import Java resources into the SampleApp application (optional depending on yourcode)
The net.rim.device.api.ui package provides the fundamental components for constructing the UI of a BlackBerry device application.

The net.rim.device.api.ui.component package provides a library of prebuilt interface components and controls for constructing a UI for a BlackBerry device application.

The net.rim.device.api.ui.container package provides a library of prebuilt interface component managers for constructing a UI for a BlackBerry device application.

The net.rim.device.api.system package provides system-level functionality.

The net.rim.device.api.i18n package provides support for the internationalization of a BlackBerry device application.

In Eclipse, in the text editor window, below the line that begins with the word package, type the names of the packages to import:
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.*;
import net.rim.device.api.i18n.*;



Your class should now look like: Since we are not using these new resources,Eclipse will warn that there are some unused imports.


package com.screv;

import net.rim.device.api.ui.UiApplication;
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.*;
import net.rim.device.api.i18n.*;

public class SampleApp extends UiApplication {

public SampleApp() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}

}



The UiApplication class is the base class for all BlackBerry device applications that provide a UI.

To provide a UI, the BlackBerry device application must extend the net.rim.device.api.ui.UiApplication class. This class provides methods for a BlackBerry device application to register event listeners, manage threads, and manage UI components.

A UI maintains a stack of screen objects. As a BlackBerry device application pushes screens onto the stack, the UI draws them on top of screens that are already on the stack. When a BlackBerry device application pops a screen off the stack, the UI redraws the underlying screens as necessary. Only the screen that is on the top of the stack receives input events.

Within the main() method, create the SampleApp object and start the event dispatcher:
SampleApp theApp = new SampleApp();
theApp.enterEventDispatcher();
The main method for the BlackBerry device application is created automatically if you select public static void main (String[ ] args) when you add new .java files to the SampleApp project.


You code should now look like


package com.screv;

import net.rim.device.api.ui.UiApplication;
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.*;
import net.rim.device.api.i18n.*;

public class SampleApp extends UiApplication {

public SampleApp() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

SampleApp theApp = new SampleApp(); //Create an instance of this class
theApp.enterEventDispatcher(); //attach an event Dispatcher


}

}

Create a screen for the Sample App application

Invoking super() invokes the constructor of the SampleAppScreen superclass, MainScreen. The MainScreen class provides a UI screen that contains a title section, a separator element, and a single vertical field manager for maintaining a list of fields.

A LabelField allows a BlackBerry device application to display a label on a UI.

A RichTextField is a read-only text field that you can use to display text in a variety of fonts and formats on a BlackBerry device screen.

Invoking add(RichTextField("Hello World!")) adds the RichTextField to the Manager object of the HelloWorldScreen screen.

Create a class that extends the MainScreen class.
final class SampleAppScreen extends MainScreen {
}Create a constructor for the SampleAppScreen class.
public SampleAppScreen(){
}
In the constructor, add the code to create and display the title and text field.
super();
LabelField title = new LabelField("Sample Application", LabelField.ELLIPSIS
| LabelField.USE_ALL_WIDTH);
setTitle(title);
add(new RichTextField("Hello World!"));




I chose to create a separate class file for the MainScreen class,it could have included in the main body of the SampleApp class.



Eclipse warns that LabelField cannot be resolved to a type, so you can right-mouse click and add the correct import statement.


package com.screv;

import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.*;

import net.rim.device.api.ui.container.MainScreen;

public class SampleAppScreen extends MainScreen {

public SampleAppScreen() {
// TODO Auto-generated constructor stub
LabelField title = new LabelField("Sample Application",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
add(new RichTextField("Hello World!"));
}

public SampleAppScreen(long arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}

}


Display a screen in the Sample App application
In the SampleApp constructor, add the code that displays a screen:
pushScreen(new SampleAppScreen());

The SampleApp constructor is created automatically if you select Constructors from superclass when you add new .java files to the Sample App project.

The pushScreen() method is inherited from the UiApplication class and pushes a screen onto the display stack and paints it. The BlackBerry Java®Virtual Machine maintains screens in a display stack.

package com.screv;

import net.rim.device.api.ui.UiApplication;
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.*;
import net.rim.device.api.i18n.*;

public class SampleApp extends UiApplication {

public SampleApp() {
// TODO Auto-generated constructor stub
pushScreen(new SampleAppScreen());
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

SampleApp theApp = new SampleApp(); //Create an instance of this class
theApp.enterEventDispatcher(); //attach an event Dispatcher


}

}
Display a message when the Hello World application closes
The onClose() method is inherited from the Screen class and indicates to a BlackBerry device application that a close event has occurred.

The Dialog.alert() method creates an alert dialog box that a BlackBerry device application can use to notify a BlackBerry device user of an event or important information. The dialog displays an exclamation mark bitmap.

In the SampleScreen class, override the onClose()method to create an alert dialog box and display text.
public boolean onClose()
{ Dialog.alert("Goodbye!");
System.exit(0);
return true;
}


package com.screv;

import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.*;

import net.rim.device.api.ui.container.MainScreen;

public class SampleAppScreen extends MainScreen {

public SampleAppScreen() {
// TODO Auto-generated constructor stub
LabelField title = new LabelField("Sample Application",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
add(new RichTextField("Hello World!"));
}

public SampleAppScreen(long arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}

public boolean onClose()
{ Dialog.alert("Goodbye!");
System.exit(0);
return true;
}
}



Test the Hello World application using the BlackBerry Smartphone Simulator
On the Run menu, click Run As > BlackBerry Simulator.
On the BlackBerry Smartphone Simulator, open the Downloads folder.
Click the Hello World application. The Sample App application displays the text Hello World!.
On the menu of the Hello World application, click Close. The Sample App application displays a dialog box with the text Goodbye!.
Click OK.




the Simulator will take a few minutes to load up, once loaded navigate to the Downloads folder to find the Sample application

You need to click on the BlackBerry button as shown below





Click on the SampleApp application




Result





Sample Code:



SampleApp.java SampleApp.java SampleAppScreen.java SampleAppScreen.java

Eclipse Project Interchange



Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/

Steve Robinson - IBM Champion 2013

About Me

Steve Robinson has been working in IT for over 20 years and has provided solutions for many large-enterprise corporate companies across the world. Steve specialises in Java and Middleware.

In January 2013, I was awarded the prestigous 'IBM Champion' accolade.


  • Linked In
  • Twitter
  • About Me
  • My Blog
  • Contact Me

Read my books?

IBM WebSphere Application Server 8.0 Administration Guide

IBM WebSphere Application Server 8.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Categories

Other Categories