ebook img

Beginning BlackBerry Development PDF

264 Pages·2009·8.467 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Beginning BlackBerry Development

Cyan yelloW MaGenTa BlaCk Books for professionals By professionals® The eXperT’s VoiCe® in BlaCkBerry Companion Beginning BlackBerry eBook Available Development Beginning Dear Reader, B Beginning BlackBerry Development will teach you how to build Java-based BlackBerry applications, starting from the very basics all the way through pack- l aging and distributing, giving you enough knowledge to produce useful, real- a world applications. Beginning I’ve worked with BlackBerry devices for almost as long as they’ve been c around and in that time have seen the platform grow to the point that you can k do a lot of great things with it – using location-based services, sound and other media, and other technologies – but a lot of developers have difficulty finding B BlackBerry good information on how to get started. I’ve seen a lot of the same kinds of questions being asked repeatedly: What tools do you use? What are the basic e components of an application? How do I do things like make a network call, or save data persistently? r All that information is out there, but just not in an easily accessible form. r That’s why I wrote this book: to give an easy to follow introduction to BlackBerry development that assumes no BlackBerry specific knowledge, and includes y enough information that by the end of the book you can build something useful. Development The book covers setup of the development environment, using the BlackBerry D device simulators, loading onto a real device, and basics of BlackBerry applica- tions. It also covers areas of the BlackBerry platform that almost all applica- e tions end up using, including networking, data persistence, and user interface. Finally it talks about how to get your applications onto users’ devices, and how v to work with App World to distribute your application to a wide audience, which is after all the goal of any application developer. e I’ve enjoyed writing this book, I hope you’ll find it useful and that it’ll help you bring your BlackBerry application ideas to life! l o Anthony Rizk Learn how to build Java-based BlackBerry p Companion eBook applications from scratch m S PleaSe note that e tl theSe RelateD titleS e See last page for details ti on $10 eBook version D aRe moCk CoveRS anD e n at we Don't have final l Re veRSionS at thiS time t Anthony Rizk SOURCE CODE ONLINE www.apress.com (cid:41)(cid:51)(cid:34)(cid:46)(cid:0)(cid:25)(cid:23)(cid:24)(cid:13)(cid:17)(cid:13)(cid:20)(cid:19)(cid:16)(cid:18)(cid:13)(cid:23)(cid:18)(cid:18)(cid:21)(cid:13)(cid:16) R (cid:21)(cid:19)(cid:25)(cid:25)(cid:25) iz k US $39.99 Shelve in Mobile Computing User level: Beginner-Intermediate (cid:25) (cid:23)(cid:24)(cid:17)(cid:20)(cid:19)(cid:16) (cid:18)(cid:23)(cid:18)(cid:18)(cid:21)(cid:16) this print for content only—size & color not accurate trim = 7.5" x 9.25" spine = 0.000" 000 page count Download at WoweBook.Com Beginning BlackBerry Development ■ ■ ■ Anthony Rizk i Download at WoweBook.Com Beginning BlackBerry Development Copyright © 2009 by Anthony Rizk All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2427-3 ISBN-13 (electronic): 978-1-4302-2428-0 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Ewan Buckingham Technical Reviewer: Paul Dumais Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow- Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Anne Collett Copy Editors: Heather Lang, Ginny Munroe and Kim Wimpsett Compositor: MacPS, LLC Indexer: BIM Indexing and e-Services Artist: April Milne Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please e-mail [email protected], or visit http://www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales– eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. ii Download at WoweBook.Com To Sabrina and Hannah –Anthony iii Download at WoweBook.Com Contents at a Glance ■Contents at a Glance..............................................................................................................iv ■Contents..................................................................................................................................v ■About the Author.....................................................................................................................x ■About the Technical Reviewer...............................................................................................xi ■Acknowlegments..................................................................................................................xii ■Setting the Stage.....................................................................................................................1(cid:1) ■Hello World............................................................................................................................17(cid:1) ■What Makes a BlackBerry Application?................................................................................47(cid:1) ■User Interface Basics............................................................................................................63(cid:1) ■Beyond the Basics of User Interfaces...................................................................................83(cid:1) ■Storing Data........................................................................................................................131(cid:1) ■Hello Out There! Making a Network-Enabled Application...................................................159(cid:1) ■Where Am I? Using Location-Based Services...................................................................193(cid:1) ■Getting Your App Out There: Packaging and Publishing.....................................................213(cid:1) ■Next Steps...........................................................................................................................237(cid:1) ■Index...................................................................................................................................239(cid:1) iv Download at WoweBook.Com Contents ■Contents at a Glance..............................................................................................................iv(cid:1) ■Contents..................................................................................................................................v(cid:1) ■About the Author.....................................................................................................................x(cid:1) ■About the Technical Reviewer...............................................................................................xi(cid:1) ■Acknowledgments................................................................................................................xii(cid:1) ■Chapter 1: Setting the Stage...................................................................................................1(cid:1) What You Need to Know Before You Can Begin......................................................................................................................................2(cid:1) Setting Up Your Development Environment.............................................................................................................................................2(cid:1) Installing the Development Environment............................................................................................................................................3(cid:1) Getting Code Signing Keys.................................................................................................................................................................9(cid:1) What’s Different About Developing for BlackBerry................................................................................................................................12(cid:1) Limited CPU and Memory.................................................................................................................................................................12(cid:1) Java as the Native API......................................................................................................................................................................12(cid:1) Limited Screen Real-Estate..............................................................................................................................................................13(cid:1) User Input.........................................................................................................................................................................................13(cid:1) Many Different Devices....................................................................................................................................................................13(cid:1) What’s in This Book...............................................................................................................................................................................13(cid:1) Chapter 2..........................................................................................................................................................................................13(cid:1) Chapter 3..........................................................................................................................................................................................14(cid:1) Chapter 4..........................................................................................................................................................................................14(cid:1) Chapter 5..........................................................................................................................................................................................14(cid:1) Chapter 6..........................................................................................................................................................................................14(cid:1) Chapter 7..........................................................................................................................................................................................14(cid:1) Chapter 8..........................................................................................................................................................................................14(cid:1) Chapter 9..........................................................................................................................................................................................14(cid:1) Chapter 10........................................................................................................................................................................................15(cid:1) v Download at WoweBook.Com ■ CONTENTS ■Chapter 2: Hello World..........................................................................................................17(cid:1) Creating an Application with the BlackBerry JDE..................................................................................................................................17(cid:1) Creating the Workspace and Project................................................................................................................................................18(cid:1) Creating the Application Classes......................................................................................................................................................19(cid:1) The Main Application Class..............................................................................................................................................................22(cid:1) Coding the Main Screen Class..........................................................................................................................................................23(cid:1) Running the Simulator......................................................................................................................................................................25(cid:1) Building and Signing Your Application.............................................................................................................................................27(cid:1) Building with Different JDE Versions................................................................................................................................................27(cid:1) Loading Hello World onto a Device...................................................................................................................................................28(cid:1) Using the Debugger..........................................................................................................................................................................28(cid:1) Creating an Application with the BlackBerry JDE Plug-in for Eclipse...................................................................................................32(cid:1) Creating the Project..........................................................................................................................................................................33(cid:1) Creating the Application Classes......................................................................................................................................................35(cid:1) Running the Simulator......................................................................................................................................................................37(cid:1) Building and Signing Your Application.............................................................................................................................................40(cid:1) Building with Different JDE Versions................................................................................................................................................40(cid:1) Debugging on a Device.....................................................................................................................................................................41(cid:1) Polishing the Application.......................................................................................................................................................................42(cid:1) Setting the Title in Project Properties...............................................................................................................................................42(cid:1) Creating an Icon...............................................................................................................................................................................43(cid:1) Seeing It All in Action.......................................................................................................................................................................46(cid:1) Summary...............................................................................................................................................................................................46(cid:1) ■Chapter 3: What Makes a BlackBerry Application?..............................................................47(cid:1) Javadocs...............................................................................................................................................................................................47(cid:1) The BlackBerry Application Life Cycle...................................................................................................................................................49(cid:1) Starting the Application....................................................................................................................................................................49(cid:1) Creating the Application...................................................................................................................................................................49(cid:1) Invoking the Event Thread................................................................................................................................................................50(cid:1) Processing Events............................................................................................................................................................................50(cid:1) Exiting the Application......................................................................................................................................................................50(cid:1) Threading and the Event Thread...........................................................................................................................................................50(cid:1) Knowing When Your Application Is on the Event Thread..................................................................................................................51(cid:1) Updating the UI from Other Threads.................................................................................................................................................51(cid:1) Running Background Applications.........................................................................................................................................................54(cid:1) Detecting Backgrounding or Foregrounding.....................................................................................................................................54(cid:1) Sending Your Application to the Background...................................................................................................................................55(cid:1) Understanding the Types of Projects.....................................................................................................................................................56(cid:1) Libraries............................................................................................................................................................................................56(cid:1) Creating an Alternate Entry Point.....................................................................................................................................................58(cid:1) MIDP and MIDlet Projects.................................................................................................................................................................61(cid:1) Summary...............................................................................................................................................................................................61(cid:1) ■Chapter 4: User Interface Basics..........................................................................................63(cid:1) The UI Fun Application...........................................................................................................................................................................63(cid:1) The Components of a BlackBerry UI......................................................................................................................................................64(cid:1) Creating the Application...................................................................................................................................................................65(cid:1) Adding the Logo Image.....................................................................................................................................................................66(cid:1) vi Download at WoweBook.Com ■ CONTENTS Adding a Few More Fields................................................................................................................................................................70(cid:1) Handling User Interaction......................................................................................................................................................................74(cid:1) Handling UI Events............................................................................................................................................................................74(cid:1) Handling the Clear Button.................................................................................................................................................................75(cid:1) Handling the Login Button................................................................................................................................................................76(cid:1) Creating Menus.....................................................................................................................................................................................78(cid:1) Understanding Menu Items..............................................................................................................................................................78(cid:1) Implementing the Login and Clear Menu Items................................................................................................................................79(cid:1) Summary...............................................................................................................................................................................................82(cid:1) ■Chapter 5: Beyond the Basics of User Interfaces..................................................................83(cid:1) Enhancing the UI Fun Application..........................................................................................................................................................83(cid:1) Working with Fonts................................................................................................................................................................................85(cid:1) Drawing to the Screen Using the Graphics Context...............................................................................................................................87(cid:1) Using the paint method....................................................................................................................................................................87(cid:1) Understanding How the BlackBerry Screen Is Drawn......................................................................................................................88(cid:1) Creating Custom Fields.........................................................................................................................................................................90(cid:1) Adding a Custom Label Field............................................................................................................................................................90(cid:1) Creating a Custom Button Field........................................................................................................................................................98(cid:1) A Review of Custom Fields.............................................................................................................................................................105(cid:1) Creating Custom Managers.................................................................................................................................................................105(cid:1) Creating a Manager........................................................................................................................................................................108(cid:1) Tidying Up the Login Success Screen............................................................................................................................................116(cid:1) Creating a Custom Screen...................................................................................................................................................................117(cid:1) Delegate Managers........................................................................................................................................................................118(cid:1) Implementing the Screen’s sublayout Method...............................................................................................................................119(cid:1) Adding a Few Fields.......................................................................................................................................................................120(cid:1) Painting the Background................................................................................................................................................................121(cid:1) Adding the Final Touches....................................................................................................................................................................122(cid:1) Adding a Header Background.........................................................................................................................................................122(cid:1) Making Minor Tweaks....................................................................................................................................................................124(cid:1) Working with the BlackBerry Storm....................................................................................................................................................125(cid:1) Creating Basic Animation....................................................................................................................................................................127(cid:1) Setting the Vertical Offset...............................................................................................................................................................127(cid:1) Animating the Layout......................................................................................................................................................................128(cid:1) Summary.............................................................................................................................................................................................129(cid:1) ■Chapter 6: Storing Data.......................................................................................................131(cid:1) Storing Data on the BlackBerry...........................................................................................................................................................131(cid:1) Choices for Persistence.......................................................................................................................................................................132(cid:1) RMS................................................................................................................................................................................................132(cid:1) Persistent Store..............................................................................................................................................................................132(cid:1) Runtime Store.................................................................................................................................................................................132(cid:1) JSR 75 FileConnection....................................................................................................................................................................133(cid:1) SQLite.............................................................................................................................................................................................133(cid:1) BlackBerry Persistent Store.................................................................................................................................................................133(cid:1) Persistent Store Keys.....................................................................................................................................................................134(cid:1) Persistent Objects..........................................................................................................................................................................134(cid:1) What Can You Persist?...................................................................................................................................................................135(cid:1) vii Download at WoweBook.Com ■ CONTENTS The Persistable Application............................................................................................................................................................136(cid:1) More Advanced Persistence...........................................................................................................................................................140(cid:1) Clearing Persistent Data from a Device..........................................................................................................................................143(cid:1) The FileConnection API........................................................................................................................................................................143(cid:1) Writing to the File System..............................................................................................................................................................152(cid:1) Summary.............................................................................................................................................................................................157(cid:1) ■Chapter 7: Hello Out There! Making a Network-Enabled Application.................................159(cid:1) Different Ways to Connect...................................................................................................................................................................159(cid:1) BES/MDS........................................................................................................................................................................................160(cid:1) Direct TCP/IP...................................................................................................................................................................................161(cid:1) BIS..................................................................................................................................................................................................162(cid:1) WiFi.................................................................................................................................................................................................162(cid:1) WAP 2.0..........................................................................................................................................................................................162(cid:1) WAP 1.0..........................................................................................................................................................................................163(cid:1) Recommendation............................................................................................................................................................................163(cid:1) Service Book........................................................................................................................................................................................163(cid:1) The MIDP Connection Framework.......................................................................................................................................................165(cid:1) Connector.......................................................................................................................................................................................165(cid:1) Connections....................................................................................................................................................................................165(cid:1) HTTP Basics....................................................................................................................................................................................166(cid:1) The Test Web Application...............................................................................................................................................................167(cid:1) The Networking Application................................................................................................................................................................168(cid:1) Some Controls................................................................................................................................................................................168(cid:1) Making an HTTP Connection...........................................................................................................................................................169(cid:1) The HttpRequestDispatcher Class..................................................................................................................................................169(cid:1) Testing It.........................................................................................................................................................................................173(cid:1) Two-Way Interaction: Sending Data via HTTP POST.......................................................................................................................179(cid:1) Connection Method: Using Direct TCP/IP Instead of BES/MDS............................................................................................................184(cid:1) Making a Connection Using WAP 2.0..............................................................................................................................................184(cid:1) Making a Connection Using BIS......................................................................................................................................................185(cid:1) Making a WiFi Connection..............................................................................................................................................................185(cid:1) Determining Network Availabilty.........................................................................................................................................................186(cid:1) Using CoverageInfo.........................................................................................................................................................................186(cid:1) Using WLANInfo..............................................................................................................................................................................186(cid:1) Putting It All Together.....................................................................................................................................................................187(cid:1) TCP Socket Connections......................................................................................................................................................................188(cid:1) Summary.............................................................................................................................................................................................191(cid:1) ■Chapter 8: Where Am I? Using Location-Based Services..................................................193(cid:1) GPS Support on BlackBerry Devices....................................................................................................................................................193(cid:1) The Location API..................................................................................................................................................................................194(cid:1) GPS Modes.....................................................................................................................................................................................194(cid:1) Specifying a GPS Mode..................................................................................................................................................................195(cid:1) The Location Application.....................................................................................................................................................................197(cid:1) Getting Periodic Location Updates Using LocationListener............................................................................................................202(cid:1) Using BlackBerry Maps.......................................................................................................................................................................205(cid:1) The Invoke API................................................................................................................................................................................206(cid:1) Launching the Default BlackBerry Maps View................................................................................................................................206(cid:1) viii Download at WoweBook.Com

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.