Tuesday, July 6, 2010

Basic Blackberry UI Program


Creative Commons Licence
This work is licenced under a Creative Commons Licence.

The Blackberry JDE workspace for this program can be obtained using Git: git clone git://git.hrbuckley.net/BB/Lesson1/

Every Java source must be part of a package. Membership in a package has an impact on class scoping rules and permissions. Objects belonging to the same package will have different access rules applied with respect to each other than objects belonging to different packages. For this reason, a standard Blackberry device will not load modules with Classes declared in restricted packages. This prevents a malicious programmer from using the scoping and access rules of package membership to bypass Blackberry security. It is traditional that the file structure of a Java project reflect the package structure, but this is not enforced by RIM's Java Development Environment (JDE). So the first decison we need to make is what Package we want out Class to belong to, and declare it in the source code:




package src.lessonOne;

Next we must import the declarations of all the classes we are going to use. Some classes seem to be automatically imported by the JDE, String for example, and need not be explicitly imported. It is probably a bad habit not to import these, but perhaps not my worst bad habit. A Blackberry program implementing a user interface must have at least one object that extends net.rim.device.api.ui.UiApplication so:



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

We can now declare our Class:



public class BasicGUI extends UiApplication {

}

Next we must tell the compiler how to start our program. The RIM compiler/linker will look for an object with a method called main declared public static void and taking a single String array as an argument. It will arrange for this method to be called when our program is launched. A minimal main method will instantiate our sub-class of UiApplication and call the inherited method enterEventDispatcher which will not normally return:



public static void main(String[] args) {
 BasicGUI basicGUI = new BasicGUI();
 basicGUI.enterEventDispatcher();
}

Add a simple constructor (which we will embelish later) and we have a complete, if not very useful Blackberry program:



/*
* BasicGUI.java
*
* © Richard Buckley www.hrbuckley.net, 2010
* 
* This work is licenced under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
* To view a copy of this licence, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to
* Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA.
*/

package src.lessonOne;

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


/**
* 
*/
public class BasicGUI extends UiApplication {
 public static void main(String[] args) {
  BasicGUI basicGUI = new BasicGUI();
  basicGUI.enterEventDispatcher();
 }


 BasicGUI() {    }
} 

The reason this program is not very useful is that, one of the things that enterEventDispatcher does, is monitor the number of Screens a UiApplication has pushed to the screen stack. We haven't create any screens at all, so the program will exit directly. In my next post we will add a simple screen to our program.

No comments:

Post a Comment