CPGM21X1 - Sheridan College

Report
PROG 38448
Mobile Java Application
Development
BlackBerry App Lifecycle
Java ME API
BlackBerry App Life Cycle
1.
Application Starts
Three ways to start an app:
User clicks app icon
App starts automatically on startup or
reboot
App is started by another app
Starting point in your application class
is main() method
7/17/2015
Wendi Jollymore, ACES
2
BlackBerry App Life Cycle
2.
Application Object Is Created
Triggered in the main() method
You instantiate your application class that
extends UiApplication
Application classes with a Ui must
extend UiApplication class
Can only have one UiApplication instance
for any application
Otherwise RuntimeException is thrown.
7/17/2015
Wendi Jollymore, ACES
3
BlackBerry App Life Cycle
3.
Event thread is started
main() is the main thread or process
that’s running
Do anything you need to do here before
you pass control to BB O/S
enterEventDispatcher() method call
Takes control of the main thread
Draws the main screen and listens for
events
Won’t return, so if you need something
else done in main(), do it first
7/17/2015
Wendi Jollymore, ACES
4
BlackBerry App Life Cycle
4.
5.
Events are processed
User events triggered by input from
trackball, touch screen, keyboard, etc
Other events
Application Exits
When the last screen is removed from
the stack.
Avoid System.exit() !!
Close all screens until the last screen is
closed.
Proper clean up of all application state
7/17/2015
Wendi Jollymore, ACES
5
What is the API?
Application Program Interface
The collection and definition of building
blocks available in a language
Descriptions of classes and their public
methods
http://en.wikipedia.org/wiki/Api
Where to read the API docs?
http://www.blackberry.com/developers/docs/
6.0.0api/index.html
Go to any JDE folder and select BlackBerry
JDE API Reference
7/17/2015
Wendi Jollymore, ACES
6
Java ME API
Java ME – Micro Edition
For developing mobile apps
Runs on custom JVM for BlackBerry
BlackBerry Java Virtual Machine
MIDP 2.0 Standard– Mobile Information
Device Profile
http://www.webopedia.com/TERM/M/
MIDP.html
Part of Java ME API used for various
mobile devices (not just for RIM)
Contains APIs for persistent storage, user
interface, networking
7/17/2015
Wendi Jollymore, ACES
7
Java ME API
CLDC – Connected Limited Device
Configuration
http://www.webopedia.com/TERM/C/C
LDC.html
A specification for a stripped down
virtual machine
For devices with very limited memory
Contains minimalist versions of
java.lang, java.util, java.io
Contains java.microedition.io for
network connections
7/17/2015
Wendi Jollymore, ACES
8
Java ME API
Other API Extensions specific to
BlackBerry devices
User Interface
Persistent Data Storage
Networking
Event Listeners
Application Integration
Additional utilities for encryption,
compression, location-based services,
etc.
7/17/2015
Wendi Jollymore, ACES
9
Using API to Create App
Simplest: Start with two classes
An application class
The main application where it all starts
Extends UiApplication class
Three main tasks to perform:
Create an instance of the app
Create main screen and push onto display stack
Start the event dispatch thread
7/17/2015
Wendi Jollymore, ACES
10
Using API to Create App
Simplest: Start with two classes
(continued)
A screen class
A visible display
Extends MainScreen class
Could have components, images, user
input, etc
Components = Fields
net.rim.device.api.ui.Field
Layout = Managers
net.rim.device.api.ui.Manager
7/17/2015
Wendi Jollymore, ACES
11
UiApplication Class
net.rim.device.api.ui.UiApplication
Child of Application class
A non-gui program would extend this
Base class for all UI applications
Contains a screen stack
Only top screen is ever visible
pushScreen(screen) adds a screen to
the stack
Lays out and prints the screen object
A screen can only be pushed onto the
stack once, or exception occurs
7/17/2015
Wendi Jollymore, ACES
12
UiApplication Class
public class MyApp extends
UiApplication {
public MyApp() {
MyScreen scr = new MyScreen();
pushScreen(scr);
}
// ...
}
7/17/2015
Wendi Jollymore, ACES
13
UiApplication Class
Event dispatcher thread
enterEventDispatcher() called on your
application class
Takes control of main thread
Handles all drawing and eventhandling
Never returns; ends when application
ends
Call this in the application’s main()
method
7/17/2015
Wendi Jollymore, ACES
14
UiApplication Class
public class MyApp extends
UiApplication {
public MyApp() {
MyScreen scr = new MyScreen();
pushScreen(scr);
}
public static void main(String[] args) {
MyApp app = new MyApp();
app.enterEventDispatcher();
}
}
7/17/2015
Wendi Jollymore, ACES
15
MainScreen Class
net.rim.device.api.ui.container.
MainScreen
Child of Screen/FullScreen class
Has a VerticalFieldManager, title area,
default menu, etc
In the MainScreen constructor is where
you’d create and lay out your main UI
7/17/2015
Wendi Jollymore, ACES
16
MainScreen Class
public class MyScreen extends MainScreen {
public MyScreen() {
LabelField label = new
LabelField(“Hello!");
add(label);
LabelField title = new
LabelField("Java ME");
this.setTitle(title);
}
}
7/17/2015
Wendi Jollymore, ACES
17
Hierarchy
It’s interesting to
note the inheritance
hierarchy of these
classes:
Object
Field
ScrollView
Manager
Screen
7/17/2015
Wendi Jollymore, ACES
18
Exercises and Homework
Exercise:
Do the exercise in the Lesson 2 notes
For next class:
Read Chapter 4 of Beginning
BlackBerry
7/17/2015
Wendi Jollymore, ACES
19

similar documents