Knocking The Door Of BlackBerry Applications

Transcription

Knocking the Door of BlackBerryApplicationsApplies to:Applies to any application to be built over the BlackBerry platform.SummaryA few years ago I worked with the old 3Com’s Palm Pilot by developing a small financial application andmore recently with the Honeywell’s Dolphin making an enhancement of the SAP’s MAM and MAU (JSPapplications). Now the challenge is BlackBerry applications. All three devices are opposite poles in terms ofhardware, OS and development functionalities so it’s been an interesting journey for me in the mobileapplications. This is the result of my little research to create a simple BlackBerry application.Author:Alvaro M. Guzmán A.Company: SAP Multi Country Latin America MCLA (branch Venezuela)Created on: January 18 2011Author BioI’ve been working in SAP since 2007 as a senior consultant. My mayor skills are focused on CRM withtechnical and functional skills but I’m also a strong web developer with knowledge in BSP, WS, Java webcomponents and more recently in mobile applications.SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com1

Knocking the Door of BlackBerry ApplicationsTable of ContentsKnocking the door of BlackBerry Applications . 3Initial considerations . 3Creating a project . 3Executing our application for the first time – BlackBerry Simulator . 5Adding UI components . 8Adding a label . 8Adding an input field . 9Adding a button . 10Adding an image . 12Adding an icon to our application . 14Conclusions . 15Related Content . 16Copyright. 17SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com2

Knocking the Door of BlackBerry ApplicationsKnocking the door of BlackBerry ApplicationsI’ve been carrying a BlackBerry phone for a while and I was wondering how difficult could be to create anapplication for it like a “Hello World” or something similar, just as a experiment. You don’t have to be avisionary to notice the boom this devices had, especially in countries like mine (Venezuela) where everyonewants to have the latest BlackBerry (even though in most of the cases they can’t afford it) and mobileapplications like social media are growing every day.So, I decide to spend some time doing this research and finally build a small (but still appealing) applicationjust to show you a “step by step” with the basics about this; then you can develop your own applications. Ifyou’re readings this is because you’re interested and willing to put your development skills to work in thisexciting platform.It is a good moment to remind you about the Java development knowledge that you must have in order toread this article and take the most benefit of it.Just before start I must say that developing for the BlackBerry platform was extremely easy and fun so Ihope you enjoy this article as much as I did written it. If you follow my recommendations you will be able tocreate your own application, installed in your phone and show your work wherever you go :)Ready? Set and go!!!Initial considerationsLet’s start with some considerations about BlackBerry development that may interest you: RIM Platform supports J2ME It uses “K” Virtual Machine (KVM), an small Java Virtual Machine (http://java.sun.com/products/cldc/wp/) BlackBerry-specific APIs that give the application a native BlackBerry look are available. Theseapplications are often called RIMlets in contrast to MIDlets /5.0.0api/index.html) UI operations take place on the event thread A RIMlet screen is not a movable window. To display it, you simply push it on the display stack usingpushScreen() method To run a standard MIDlet on a BlackBerry device, jad and jar files have to be converted to jads and cods,a proprietary formatCreating a projectFor my development I used the BlackBerry Java DE v5, which runs over JSDK1.6 (32 bits) even though rdev/devtoolsdownloads.jspSo, once you install the software the first task to be performed is to create a project; in our case I named it as“MyFirstBBApp” as follows:SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com3

Knocking the Door of BlackBerry ApplicationsA new application is created in the selected folder including the necessary files for the compilation (at thispoint no Java component were created).Now, create a new Java class, for example with the name “Main.java”. This can be done by clicking the rightbutton over the project and select “Create new file in project”This class must extend from class net.rim.device.api.ui.UiApplication which comes from the original RIM’sAPI and provide the base for all applications that has a UI interface:The UIApplication class works as a stack of screens objects; it pushes the screens onto the stack and onlydraws the screen on top of all. You’ll see more of that in the next sections.In our Main class a main method need to be created in order to eventually execute our application in thephone:Another Java class needs to be created for our example with name of “Screen.java”. This class must extendfrom net.rim.device.api.ui.container.MainScreen which also comes in the RIM’s API:SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com4

Knocking the Door of BlackBerry ApplicationsThe MainScreen class acts as a canvas that includes the title section, a separator element, the mainscrollable section and it is in charge of the events. We’re going to use this class to draw all the UI objects wewant to add in our application.Once we create our class we’re able to assign a title to our application by executing the method setTitle inthe constructor as follows:Our Screen class now can be instantiate it on the Main class; to do this we must create an init method likethis and create the instance (don’t forget the pushScreen method call!):Finally we call our init method from the main method and add the enterEventDispatcher method call, whichis in charge of execute all the drawing and event-handling so don’t forget to include it, if you do, yourapplication just won’t start:After this, we must now Build our project and generate the ALX file (which is the application XMLconfiguration file). Once the build was executed successfully please check the following files in theapplication folder:MyFirstBBApp.alx (configuration file indicating the project’s properties)MyFirstBBApp.cod (contain compiled java code and scripts – BlackBerry proprietary)Everything is in place? Now we can execute our application!!! Executing our application for the first time – BlackBerry SimulatorIt is a good idea to do this on a simulator first (you don’t want to crash your phone isn’t?). For this use mulators.jsp. There is a specific installer for every simulatordepending on the BlackBerry device you want to use.Note: In Windows 7 there is a small bug in the simulator you can never close the program, which means you need tokill the process using the Window’s Task Manager. This may caused you troubles if you do this with regularity inwhich case you would be force to re install the simulator.SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com5

Knocking the Door of BlackBerry ApplicationsOnce you install the simulator it could looks like this (depending on the model you choose):SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com6

Knocking the Door of BlackBerry ApplicationsIn order to load our application into the Simulator just select “Load Java Program” from the menu and selectour “MyFirstBBApp.cod” from the project’s folder:If everything was OK we should be able to see our application icon in the “Downloads” folder of the phone(the boring icon will be change later):Note: The name of our application could be change in the ALX file.SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com7

Knocking the Door of BlackBerry ApplicationsWell click it and see our little application running!!!The screen is blank because at this point we only add the title; even though the menu is available with theoption “close” that closes our program. This behavior comes with all the applications so no additional codingis necessary.Adding UI componentsNext step will add a few UI components to our application. The RIM’s API provides the necessary libraries tobuild almost any application including all the objects related to the UI. In our example we’re going to includethe following objects: Label Input field Button ImageAdding a labelTo adding a simple Label into our application I just create a new method called paintLabel in the Screenclass as follows (where message is the text to display):As you can see I’m using the add method of the net.rim.device.api.ui.Screen class, which adds an objecttype Field that is the base for all UI components like in this case: RichTextField. Check thenet.rim.device.api.ui.component package to see the list of available classes, many of them will be used in ourexample.Now, in the init method in the Main class we add the following code in order to see three labels:SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com8

Knocking the Door of BlackBerry ApplicationsAfter these changes were made we proceed to Build the project again (a new .cod file supposes to begenerated) and execute our application; our new labels are shown:Adding an input fieldLet’s go now with the input field; to add a new one into our application I create a new method calledpaintField in the Screen class that uses the EditField class as follows:Once again, in the init method of the Main class I add the following code to add our field:Note that the initial value “Type your name here” could be change once we put the cursor over and typesomething different.SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com9

Knocking the Door of BlackBerry ApplicationsAdding a buttonWhat about a button? Check the following method:As you can notice I’m using the ButtonField class that receives the label and in the style parameter putCOMSUME CLICK indicating that our objet will consume the click event.As usual, in the init method I add the corresponding call and test it:At this point we need to add the event functionality to our program so we could handle the click event andperform the corresponding actions. For this it is necessary to implement the interfacenet.rim.device.api.ui.FieldChangeListener (which handles all the Field changes events) in the Main class.SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com10

Knocking the Door of BlackBerry ApplicationsThe FieldChangeListener forces us to implement the method fieldChanged as follows:Here you can check the button’s label to execute the desire functionality.Note: You may find a lousy solution to use the button’s label to identify the button but at the time I wrote this article Icouldn’t found a nicer approach. I’m sure you’ll find a better implementation for this.As you may notice I’m using the Dialog class which basically shows a little pop up in our application that it isgoing to appear once we add our listener to our class and click the button. What I did was add an inputparameter to the Screen class constructor called listener which is basically an instance of theFieldChangeListener interface implementation class and add a new private attribute that is going to keep it:Then, in the paintButton method we can add the listener using the setChangeListener of the Field classas shown:SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com11

Knocking the Door of BlackBerry ApplicationsIf you can remember, the Main class implements this interface so we can use it as our listener. Finally ourinit method of the Main class will look like this (look the call to the constructor):So, when we click the “Show me more” button we will see the dialog with the text we want:Adding an imageSo far we create our application, add labels, button and manage events. What about something cooler asadd an image. For this, of course, we need an image file and added to our project; in the case of ourexample the file is sap logo.gif as follows:Note: The size of these files may vary; in our case it is about 700 x 300 px but be aware that not all the BlackBerryphones has the same screen size.SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com12

Knocking the Door of BlackBerry ApplicationsIn order to show this image in our application checks the method paintImage:The class BitmapField is in charge of display a bitmap like an image so it fits our needs once we set theimage name in the method getBitmapResource. Now, we need to add a new button called “Show me theimage” and add a new condition to the method fieldChanged in order to show the image once this button isclicked:SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com13

Knocking the Door of BlackBerry ApplicationsAdding an icon to our applicationCool isn’t? :) The only thing left is change that lousy icon. For that just create an image about 80 x 80 px andadd it to the project (in our example sap logo ico.png). Then check project properties and in the tab“Resources” add the icon name and Build the project one more time:After add it again to the simulator you’ll see something like this:SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com14

Knocking the Door of BlackBerry ApplicationsConclusionsFinally, we can add our application into an actual BlackBerry phone using the Blackberry Desktop Softwarewhere we can add/remove application. Just click “Import files ” at the Application section, select fileMyFirstBBApp.alx and “Apply”.That’s all for now guys, as I said before it is quite easy to develop a simple application for a BlackBerrydevice and I’m confident you will create wonderful things starting from here.SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com15

Knocking the Door of BlackBerry ApplicationsRelated ContentMobilizing SAP on BlackBerry SmartphonesBlackBerry application integration - SAPBlackBerry development approachesSAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com16

Knocking the Door of BlackBerry ApplicationsCopyright Copyright 2011 SAP AG. All rights reserved.No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG.The information contained herein may be changed without prior notice.Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors.Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation.IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9,iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server,PowerVM, Power Architecture, POWER6 , POWER6, POWER5 , POWER5, POWER, OpenPower, PowerPC, BatchPipes,BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX,Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered trademarks of IBM Corporation.Linux is the registered trademark of Linus Torvalds in the U.S. and other countries.Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe SystemsIncorporated in the United States and/or other countries.Oracle is a registered trademark of Oracle Corporation.UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks ofCitrix Systems, Inc.HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C , World Wide Web Consortium, MassachusettsInstitute of Technology.Java is a registered trademark of Sun Microsystems, Inc.JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented byNetscape.SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP Business ByDesign, and other SAP products and services mentionedherein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries.Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, andother Business Objects products and services mentioned herein as well as their respective logos are trademarks or registeredtrademarks of Business Objects S.A. in the United States and in other countries. Business Objects is an SAP company.All other product and service names mentioned are the trademarks of their respective companies. Data contained in this documentserves informational purposes only. National product specifications may vary.These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("SAPGroup") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors oromissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in theexpress warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting anadditional warranty.SAP COMMUNITY NETWORK 2011 SAP AGSDN - sdn.sap.com BPX - bpx.sap.com BOC - boc.sap.com UAC - uac.sap.com17

Knocking the Door of BlackBerry Applications Applies to: Applies to any application to be built over the BlackBerry platform. Summary A few years ago I worked with the old 3Com’s Palm Pilot by developing a small financial application and more recently with the Honeywell’s Do