2. AGENDA
– BlackBerry Platform Overview
– Developing for BlackBerry
• Application Basics • Http Connections
• User Interfaces • Persistence
– Screens, Fields and • Background Applications
Managers
• Controlled APIs
– Ui Event Handlers
– Menus and Menu Items
– Questions
3. BlackBerry Platform Overview
Email
Internet
HTTP(S)
HTTP(S) BlackBerry App
Server with Server
MDS
Firewall
Web Services Web Server
• Security • Multiple Carrier/Network Support
• Standards Based Connectivity • JAVA™ Based Wireless Handhelds
• HTTP(s)/TCP • J2ME Compliant
• Wireless ‘Push’ • BlackBerry Browser, SDK & APIs
4. BlackBerry Platform Overview
• Browser Based Applications
• Use the BlackBerry Browser to render and display data
• Support for HTML, xHTML, WML and WML Script (support for
JavaScript in 4.0)
• Support for PUSH
• Custom Java Applications
• Written in Java using J2ME and BlackBerry APIs
• Custom user interface
• Able to store and process data
• Able to make HTTP/TCP connections to the server
• Supports background applications and PUSH
5. AGENDA
– BlackBerry Platform Overview
– Developing for BlackBerry
• Application Basics • Http Connections
• User Interfaces • Persistence
– Screens, Fields and • Background Applications
Managers
• Controlled APIs
– Ui Event Handlers
– Menus and Menu Items
– Questions
6. Developing for BlackBerry
• Application Basics
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.*;
class HelloWorld extends UiApplication{
public static void main(String[] args){
HelloWorld myApp = new HelloWorld();
myApp.enterEventDispatcher();
}
public HelloWorld(){
MainScreen screen = new MainScreen();
screen.add(new RichTextField(“Hello World!”));
pushScreen(screen);
}
}
7. Developing for BlackBerry
• Application Basics
public HelloWorld(){
MainScreen screen = new MainScreen();
screen.add(new RichTextField(“Hello World!”));
pushScreen(screen);
}
• Typically, the constructor for the main class will set up the
initial screen of the user interface
• Once the first screen is displayed the application can respond
to user input from the trackwheel or the keyboard
• Add a screen to the stack using “pushScreen(Screen s)”
8. Developing for BlackBerry
• Application Basics
public static void main(String[] args){
HelloWorld myApp = new HelloWorld();
myApp.enterEventDispatcher();
}
• The “main” method is called when the application is
instructed to start.
• The system passes in any arguments defined in the project
properties
• Typically, creates the main class and calls
“enterEventDispatcher()” to start receiving and sending Ui
events
• Under normal circumstances “enterEventDispatcher()” does
not return.
9. Developing for BlackBerry
• Application Basics
class HelloWorld extends UiApplication{
• Any application that does any Ui work must extend either the
UiApplication class or the Application class
10. Developing for BlackBerry
• Application Basics
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.*;
class HelloWorld extends UiApplication{
public static void main(String[] args){
HelloWorld myApp = new HelloWorld();
myApp.enterEventDispatcher();
}
public HelloWorld(){
MainScreen screen = new MainScreen();
screen.add(new RichTextField(“Hello
World!”));
pushScreen(screen);
}
}
11. AGENDA
– BlackBerry Platform Overview
– Developing for BlackBerry
• Application Basics • Http Connections
• User Interfaces • Persistence
– Screens, Fields and • Background Applications
Managers
• Controlled APIs
– Ui Event Handlers
– Menus and Menu Items
– Questions
12. Developing for BlackBerry
• User Interfaces
• BlackBerry uses a Java Swing-like extendable user
interface API.
• All BlackBerry Ui Classes are members of
• net.rim.device.api.ui
• net.rim.device.api.ui.container
• net.rim.device.api.ui.component
• *Note* BlackBerry and MIDP Ui Classes are mutually
exclusive, they can not be used in the same
application.
13. Developing for BlackBerry
• User Interfaces
• Field
• Lowest Level UI Component
Field
Field
• Capable of it’s own layout and
painting
• Manager
• Contains Fields Manager
Manager
• Handles layout and focus for all
contained fields
• Handles scrolling.
Screen
Screen
• Screen
• Must have a delegate manager
• Can be added to or removed from
the display stack.
14. Developing for BlackBerry
• User Interfaces – Field
• All displayable elements, except Menu and
MenuItem, extend field and implement methods
such as paint to display data in a specific way.
• Some field types include:
RichTextField Displays formatted text
EditField Displays text which can be edited by the user
ListField Displays a list of elements, each of which can be
selected
Bitmap Field Displays a bitmap
DateField Displays the day, month and year. Each of which
can be changed
15. Developing for BlackBerry
• User Interfaces – Manager
• Extends field, this means that a manager is
displayable.
• Managers ‘layout’ child fields in a specific way and
instructs them to paint themselves. They also
handle any scrolling of the fields
VerticalFieldManager Lays out child fields vertically
HorizontalFieldManager Lays out child fields horizontally
FlowFieldManager Lays out child fields horizontally then vertically when
it runs out of space on the current ‘line’
DialogFieldManager Lays out an icon and a message left to right, then
user fields vertically.
16. Developing for BlackBerry
• User Interfaces – Screen
• Extends Manager and hence can be displayed
• Screens are ‘pushed’ or ‘popped’ onto and off of the
Ui Stack. The screen on the top of the stack is
currently visible.
• Screens employ a delegate Manager to handle
layout and scrolling.
• Certain screens have a built in layout manager while
others must have one specified at instantiation
17. Developing for BlackBerry
• User Interfaces – Screen
• Screen types include
Screen
Screen
FullScreen
FullScreen PopupScreen
PopupScreen
MainScreen
MainScreen Dialog
Dialog Status
Status
18. Developing for BlackBerry
• User Interfaces
• How do I implement an Interface?
public HelloWorld(){
MainScreen screen = new MainScreen();
screen.add(new RichTextField(“Hello
World!”));
pushScreen(screen);
}
• MainScreen has a built in delegate manager which it uses to
layout the fields.
• When we add a field to the screen, it simply passes it to it’s
delegate manager for layout.
19. Developing for BlackBerry
• User Interfaces
• A more complicated layout:
public HelloWorld(){
MainScreen screen = new MainScreen();
screen.setTitle(new LabelField("Hello World!"));
FlowFieldManager flowManager = new FlowFieldManager();
screen.add(flowManager);
ButtonField button1 = new ButtonField("Button 1");
ButtonField button2 = new ButtonField("Button 2");
ButtonField button3 = new ButtonField("Button 3");
flowManager.add(button1);
flowManager.add(button2);
flowManager.add(button3);
screen.add(new SeparatorField());
screen.add(new RichTextField("This is a Hello World! "
+"Application with some
buttons"));
pushScreen(screen);
}
21. Developing for BlackBerry
• User Interfaces
• Building custom Fields
• Extend Field
• Implement (at least) the layout and paint abstract
methods.
• The getPreferredWidth and getPreferredHeight methods
should also be implemented for proper layout in some
managers.
• Building custom Managers
• Extend Manager and implement (at least) sublayout,
getpreferredHeight and getPreferredWidth.
• sublayout should invoke each field's setPosition and
layout methods
22. Developing for BlackBerry
• User Interfaces
• Event Handlers
• Listener-based approach
• The Ui can listen for field and
focus changes, scrolling,
trackwheel and keyboard
events
• A listener must implement the
appropriate listener interface.
•Ex: FieldChangeListener
23. Developing for BlackBerry
• User Interfaces
• Event Handlers
ButtonField button1 = new ButtonField("Button 1");
MyListener listener = new MyListener();
button1.addChangeListener(listener);
class MyListener implements FieldChangeListener{
public void fieldChanged(Field field, int context){
ButtonField button = (ButtonField)field;
final String label = button.getLabel();
UiApplication.getUiApplication().invokeLater(new
Runnable(){
public void run(){
Status.show(label + " was pressed");
}
});
}
}
25. Developing for BlackBerry
• User Interfaces
• Event Handlers
• The TrackwheelListener and KeyListener
interfaces are the most commonly
implemented
• Found in net.rim.device.api.system
• Allow a field to respond to user input events.
• Added to a Screen object
• Are able to but do not necessarily consume
the events
26. Developing for BlackBerry
• User Interfaces
• Event Handlers
class MyTrackwheelListener implements TrackwheelListener{
public boolean trackwheelClick(int status, int time){
UiApplication.getUiApplication().invokeLater(new Runnable(){
public void run(){
Status.show("The Trackwheel was clicked");
}
});
return true;
}
public boolean trackwheelRoll(int amount, int status, int
time){
return false;
}
public boolean trackwheelUnclick(int status, int time){
return false;
}
}
28. Developing for BlackBerry
• User Interfaces
• Menus and MenuItems
• Menu displays a menu in the top right corner of
the screen. A menu is a vertically arranged list
of items.
• A menu can display itself by calling it’s show
method
• MenuItems are runnable objects which are
added to the menu.
• The selected MenuItem is run by the system
when the trackwheel is clicked.
29. Developing for BlackBerry
• User Interfaces
• Menus and MenuItems
public boolean trackwheelClick(int status, int time){
Menu menu = new Menu();
MenuItem closeItem = new MenuItem("Close", 1000, 1000){
public void run(){
UiApplication.getUiApplication().invokeLater(new
Runnable(){
public void run(){
UiApplication.getUiApplication().popScreen(
}
});
System.exit(0);
}
};
menu.add(closeItem);
menu.show();
}
31. Developing for BlackBerry
• User Interfaces
• Fields, Managers and Screens
• Fields display data
• Managers layout Fields and handle scrolling
• Screens have a delegate Manager and are added to the Ui Stack
and are made visible
• Ui Event Handlers
• Listeners for field and focus changes
• Use TrackwheelListeners and KeyListeners to perform actions
based on user input
• Menus and Menu Items
• Runnable MenuItems are added to Menus
• The system runs the MenuItem when the user selects it from a
Menu
32. AGENDA
– BlackBerry Platform Overview
– Developing for BlackBerry
• Application Basics • Http Connections
• User Interfaces • Persistence
– Screens, Fields and • Background Applications
Managers
• Controlled APIs
– Ui Event Handlers
– Menus and Menu Items
– Questions
33. Developing for BlackBerry
• Connecting to the Network
• BlackBerry can use MDS to connect to the Intranet or the
Internet.
• HTTP(s) and TCP (socket) connections are supported
Internet
HTTP(S)
HTTP(S) BlackBerry App
Server with Server
MDS
Firewall
Web Services Web Server
34. Developing for BlackBerry
• Connecting to the Network
• The java.microedition.io classes are used to
connect to the network
• All connections should be executed on a separate
thread to avoid blocking other applications.
Internet
HTTP(S)
HTTP(S) BlackBerry App
Server with Server
MDS
Firewall
Web Services Web Server
35. Developing for BlackBerry
• Connecting to the Network
• HTTP Connections
Thread t = new Thread(new Runnable(){
public void run(){
try {
// setup the connection
HttpConnection http =
(HttpConnection)Connector.open(URL);
http.setRequestMethod(requestMethod);
• create the connection on a new Thread
• use Connector.open to establish the connection,
cast this connection to an HTTPConnection
• Set the request method and any request headers
36. Developing for BlackBerry
• Connecting to the Network
• HTTP Connections
DataOutputStream out = null;
int length = data.length();
String slength = String.valueOf(length);
http.setRequestProperty("Content-Length", slength);
try {
out = http.openDataOutputStream();
out.write(data.getBytes());
} catch (IOException ioe) {
//Handle any exceptions which may be thrown
}
out.close(); // close the output connection.
• If there is any data to send to the server, write it to
the output stream.
37. Developing for BlackBerry
• Connecting to the Network
• HTTP Connections
int received;
InputStream in = http.openInputStream();
StringBuffer response = new StringBuffer();
while((received = in.read()) != -1){
response.append((char) received);
}
in.close(); // close the input stream
• Use an InputStream to get the response from the
server.
• Get the response headers if needed at this point
38. Developing for BlackBerry
• Connecting to the Network
• HTTP Connections
int httpStatus = http.getResponseCode();
//close the connections
http.close(); // close the HTTP connection
//do something with the response data and code
}catch(IOException ioe) {
//handle any exception which may have been thrown
}
}
});
t.start();
• Finally, get the HTTP response code from the server
and close the connection.
39. AGENDA
– BlackBerry Platform Overview
– Developing for BlackBerry
• Application Basics • Http Connections
• User Interfaces • Persistence
– Screens, Fields and • Background Applications
Managers
• Controlled APIs
– Ui Event Handlers
– Menus and Menu Items
– Questions
40. Developing for BlackBerry
• Persistence
• Any application can save data in BlackBerry’s non-
volatile flash memory. That data will persist across
device resets
• Any object which is marked ‘Persistable’ can be
stored.
• All API data types are Persistable
41. Developing for BlackBerry
• Persistence
class MyDataObject implements Persistable{
// implementation of myDataObject
}
• To be persistable a data class only needs to
implement the Persistable interface.
• Persistable has no methods or variables
42. Developing for BlackBerry
• Persistence
PersistentObject persist =
PersistentStore.getPersistentObject(fileName);
persist.setContents(myDataObject);
• To save an object it simply needs to be added to the
persistent store using a unique filename.
PersistentObject persist =
PersistentStore.getPersistentObject(fileName);
MyDataObject myDataObject = (MyDataObject)persist.getContents();
• It can then be retrieved using that same filename
• Persist.getContents() returns a reference to the Persistable
object so any changes that are made to it at this point will be
automatically saved when the application exits.
44. AGENDA
– BlackBerry Platform Overview
– Developing for BlackBerry
• Application Basics • Http Connections
• User Interfaces • Persistence
– Screens, Fields and • Background Applications
Managers
• Controlled APIs
– Ui Event Handlers
– Menus and Menu Items
– Questions
45. Developing for BlackBerry
• Background Applications
• Since BlackBerry is mutli-threaded it supports
running processes in the background
• These can be auto-run on startup or created by
another application
• Any process which could take more then a second to run
should be executed on a separate thread
• Background PUSH listeners are the most common
type of background application.
46. Developing for BlackBerry
• Background Applications
• Creating a background PUSH Listener
• Build an application which starts a Listener thread. Mark
that application as “auto-run at startup” and “System
Module”.
47. Developing for BlackBerry
• Background Applications
• Creating a background PUSH Listener
• The system will run this applications main method when
the handheld boots up.
• The main method can start the listener thread
public static void main(String[] args){
//start the background listener
ListenerThread t = new ListenerThread();
t.start();
}
48. Developing for BlackBerry
• Background Applications
• Creating Alternate Application Entry Points
• Create a project with just an application icon
• Make that project an ‘Alternate CLDC Application Entry Point’
for the main app.
49. Developing for BlackBerry
• Background Applications
• Creating Alternate Application Entry Points
• The argument for the Alternate entry point gets passed
into the applications main method when the user clicks
on the application icon.
public static void main(String[] args){
if (args != null && args.length > 0 && args[0].equals("gui"))
{
//start the UI
BBTaskServerMain app = new BBTaskServerMain();
app.enterEventDispatcher();
}
}
50. Developing for BlackBerry
• Background Applications
• Creating Alternate Application Entry Points
• Put them together and the application can run the background
listener on startup, and the Ui when the user selects the
application Icon
public static void main(String[] args){
if (args != null && args.length > 0 && args[0].equals("gui"))
{
//start the UI
BBTaskServerMain app = new BBTaskServerMain();
app.enterEventDispatcher();
}else{
//start the background listener
ListenerThread t = new ListenerThread();
t.start();
}
}
51. AGENDA
– BlackBerry Platform Overview
– Developing for BlackBerry
• Application Basics • Http Connections
• User Interfaces • Persistence
– Screens, Fields and • Background Applications
Managers
• Controlled APIs
– Ui Event Handlers
– Menus and Menu Items
– Questions
52. Developing for BlackBerry
• Controlled APIs
• Any API which has the ability to access the system, the
hardware, the crypto classes or any other applications data
are signed.
• The signed APIs are marked in the Java Docs
• Any developer can use the signed APIs and run applications
which use them under simulation
• To run such an application on the handheld the application
itself must be signed by the RIM signing servers.
53. Developing for BlackBerry
• Controlled APIs
• Once the application
has been built the
JDE can be used to
request signatures
for the application.
54. Developing for BlackBerry
• Controlled APIs
• The signature tool uses a hash of the application to
generate proper signatures. BlackBerry checks for
these signatures before it lets the application
access a signed API.