BlackBerry Basic

541 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
541
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

BlackBerry Basic

  1. 1. Yellow PepperMobile Financial SolutionsBLACKBERRY BASIC September 2010
  2. 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. 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. 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. 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. 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. 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. 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. 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. 10. Developing for BlackBerry• Application Basicsimport 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(“HelloWorld!”)); pushScreen(screen); }}
  11. 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. 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. 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. 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 textEditField Displays text which can be edited by the userListField Displays a list of elements, each of which can be selectedBitmap Field Displays a bitmapDateField Displays the day, month and year. Each of which can be changed
  15. 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 fieldsVerticalFieldManager Lays out child fields verticallyHorizontalFieldManager Lays out child fields horizontallyFlowFieldManager 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. 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. 17. Developing for BlackBerry• User Interfaces – Screen • Screen types include Screen Screen FullScreen FullScreen PopupScreen PopupScreen MainScreen MainScreen Dialog Dialog Status Status
  18. 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. 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 somebuttons")); pushScreen(screen); }
  20. 20. Developing for BlackBerry• User Interfaces • A more complicated layout:
  21. 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 fields setPosition and layout methods
  22. 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. 23. Developing for BlackBerry• User Interfaces • Event HandlersButtonField 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(newRunnable(){ public void run(){ Status.show(label + " was pressed"); } }); }}
  24. 24. Developing for BlackBerry• User Interfaces • Event Handlers
  25. 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. 26. Developing for BlackBerry• User Interfaces • Event Handlersclass 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, inttime){ return false; } public boolean trackwheelUnclick(int status, int time){ return false; }}
  27. 27. Developing for BlackBerry• User Interfaces • Event Handlers
  28. 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. 29. Developing for BlackBerry• User Interfaces • Menus and MenuItemspublic boolean trackwheelClick(int status, int time){ Menu menu = new Menu(); MenuItem closeItem = new MenuItem("Close", 1000, 1000){ public void run(){ UiApplication.getUiApplication().invokeLater(newRunnable(){ public void run(){ UiApplication.getUiApplication().popScreen( } }); System.exit(0); } }; menu.add(closeItem); menu.show();}
  30. 30. Developing for BlackBerry• User Interfaces • Menus and MenuItems
  31. 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. 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. 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. 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. 35. Developing for BlackBerry• Connecting to the Network • HTTP ConnectionsThread 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. 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. 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. 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. 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. 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. 41. Developing for BlackBerry• Persistenceclass 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. 42. Developing for BlackBerry• PersistencePersistentObject 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.
  43. 43. Developing for BlackBerry• Persistence • A more robust implementationMyDataObject myDataObject;PersistentObject persist =PersistentStore.getPersistentObject(fileName);Object storedObject = persist.getContents();if(storedObject == null || !(storedObject instanceofMyDataObject)){ persist.setContents(new MyDataObject()); persist.commit(); myDataObject = (MyDataObject)persist.getContents();} else { myDataObject = (MyDataObject)storedObject;}
  44. 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. 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. 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. 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 threadpublic static void main(String[] args){ //start the background listener ListenerThread t = new ListenerThread(); t.start();}
  48. 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. 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. 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 Iconpublic 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. 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. 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. 53. Developing for BlackBerry• Controlled APIs • Once the application has been built the JDE can be used to request signatures for the application.
  54. 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.
  55. 55. Q&A

×