BlackBerry App Lifecycle Java ME API
190 likes | 338 Vues
BlackBerry App Lifecycle Java ME API. BlackBerry App Life Cycle. 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.
BlackBerry App Lifecycle Java ME API
E N D
Presentation Transcript
BlackBerry App Lifecycle Java ME API
BlackBerry App Life Cycle • 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 Wendi Jollymore, ACES
BlackBerry App Life Cycle • 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. Wendi Jollymore, ACES
BlackBerry App Life Cycle • 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 Wendi Jollymore, ACES
BlackBerry App Life Cycle • 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 Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
Java ME API • CLDC – Connected Limited Device Configuration • http://www.webopedia.com/TERM/C/CLDC.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 Wendi Jollymore, ACES
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. Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
UiApplication Class public class MyApp extends UiApplication { public MyApp() { MyScreen scr = new MyScreen(); pushScreen(scr); } // ... } Wendi Jollymore, ACES
UiApplication Class • Event dispatcher thread • enterEventDispatcher() called on your application class • Takes control of main thread • Handles all drawing and event-handling • Never returns; ends when application ends • Call this in the application’s main() method Wendi Jollymore, ACES
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(); } } Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
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); } } Wendi Jollymore, ACES
Hierarchy • It’s interesting to note the inheritance hierarchy of these classes: Wendi Jollymore, ACES
Exercises and Homework • Exercise: • Do the exercise in the Lesson 2 notes • For next class: • Read Chapter 4 of Beginning BlackBerry Wendi Jollymore, ACES