Karl G. Kowalski
RSA, The Security Division of EMC
Learn to:
• Download and work with the JDE
• Take advantage of developer tools,
including device and service simulators
• Create your own BlackBerry apps
• Submit your apps to BlackBerry App
World and get them accepted
BlackBerry
®
Application Development
Making Everything Easier!
™
Visit the companion Web site at www.dummies.com/
go/blackberryappdev to find source code files for the
code listings in the book.
Open the book and find:
• The tools you need to start
programming apps
• How to decide which JDE version
to download and use
• Pointers for creating menus and
screens
• Hints for using threads
• How to use different types of
storage in your app
• How to use simulators and real
devices to test your app
• Tips to get your app accepted to
BlackBerry App World
• Put the finishing touches on your app — debug your application
both on a simulator and on a real device
• Sell your app — deploy your app to BlackBerry App World and
upgrade it down the road
BlackBerry
®
Application
Development
Kowalski
Spine: .816”
www.it-ebooks.info
Spine: .816”
Start with FREE Cheat Sheets
Cheat Sheets include
• Checklists
• Charts
• Common Instructions
• And Other Good Stuff!
Get Smart at Dummies.com
Dummies.com makes your life easier with 1,000s
of answers on everything from removing wallpaper
to using the latest version of Windows.
Check out our
• Videos
• Illustrated Articles
• Step-by-Step Instructions
Plus, each month you can win valuable prizes by entering
our Dummies.com sweepstakes. *
Want a weekly dose of Dummies? Sign up for Newsletters on
• Digital Photography
Application Development
FOR
DUMmIES
‰
01_467114-ffirs.indd i01_467114-ffirs.indd i 8/30/10 1:03 PM8/30/10 1:03 PM
www.it-ebooks.info
BlackBerry
®
Application Development For Dummies
®
Published by
Wiley Publishing, Inc.
111 River Street
Hoboken, NJ 07030-5774
www.wiley.com
Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or
by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permit-
ted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written
permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the
Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600.
Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley
& Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://
www.wiley.com/go/permissions.
Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the
Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, Making Everything
Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/
or its af liates in the United States and other countries, and may not be used without written permission.
About the Author
Karl Kowalski has traveled the world of software development for far longer
than he really wants to remember. He has written code for everything from
airplanes, to voice recognition, to robot submarines, to games, and even
particle accelerators, and he has developed software on everything from
mainframes to cellphones. He lives near Boston and works for RSA, the
Security Division of EMC, where his tasks include developing security
solutions for mobile platforms, most especially the BlackBerry smartphone.
In his spare time, he develops software for smartphones such as BlackBerry,
iPhone, and Android as part of his startup, BlazingApps LLC (www.blazing
apps.com).
Dedication
To my parents, Stanley and Constance Kowalski, who are always there for
me, and who at every step helped me to become who I am today. Thanks,
Dad, for introducing me to programming computers, back before I could do
algebra. To my siblings — Lee Anne, Rosemarie, and Joseph — who always
kept me honest about taking time off from writing. Special thanks to Lee Anne
who helped me get started when I rst mentioned the idea. To my friend
Pauline, who saw me through some of the hard parts of becoming a writer.
Finally, to the members of the RSA Credentials Everywhere team: I couldn’t
have done this without your encouragement and support.
Author’s Acknowledgments
Many thanks go to Carole Jelen, agent extraordinaire, who never gave up on
me nor let me give up on myself. Acquisitions Editor Katie Mohr helped me
greatly through my learning to write For Dummies experience and also with
ideas and motivation for moving ahead. Project Editor Jean Nelson deserves
enormous thanks for putting up with a wet-behind-the-ears writer as I worked
to stay on target and stick to the schedule. Senior Copy Editor Teresa Artman
was very helpful in her efforts to take my typing and turn it into something
readable. Special thanks also to Leah Cameron for her feedback as I learned
Kristie Rees
Layout and Graphics: Samantha K. Cherolis,
Joyce Haughey, Christin Swinford
Proofreaders: Melissa D. Buddendeck,
Dwight Ramsey
Indexer: BIM Indexing & Proofreading Services
Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
Mary Bednarek, Executive Acquisitions Director
Mary C. Corder, Editorial Director
Publishing for Consumer Dummies
Diane Graves Steele, Vice President and Publisher
Composition Services
Debbie Stailey, Director of Composition Services
01_467114-ffirs.indd iv01_467114-ffirs.indd iv 8/30/10 1:03 PM8/30/10 1:03 PM
www.it-ebooks.info
Contents at a Glance
Introduction 1
Part I: Getting Started on BlackBerry Apps 7
Chapter 1: Gathering What You Need to Develop BlackBerry Apps 9
Chapter 2: Registering and Downloading 33
Chapter 3: Coding with the BlackBerry Java Development Environment 59
Part II: BlackBerry Application Development 77
Chapter 4: Designing and Organizing Your BlackBerry App 79
Chapter 5: Setting Up Screens and User Interfaces 109
Chapter 6: Storing Your Users’ Data 137
Chapter 7: Getting Tied Up in Threads 163
Part III: Developing Enterprise-Class BlackBerry Apps 197
Chapter 8: Writing Apps for the Enterprise 199
Discovering Apps, BlackBerry Style 11
Getting familiar with standard apps 11
Understanding how users navigate and use
their BlackBerry smartphones 12
Filling Your Toolbox 18
Downloading the software you need 18
Gathering BlackBerry simulators 19
Building or buying a development computer 20
Choosing a BlackBerry device 22
Using Your Programming Skills 23
Java programming for BlackBerry 24
Debugging 25
Using software patterns 25
Understanding BlackBerry Application Development Challenges 27
Choosing an OS version 27
Programming defensively 29
Entering a Brave, New BlackBerry App World 30
Deciding what kind of app to create 31
Brainstorming, alone or in groups 31
Becoming a BlackBerry developer and App World vendor 32
Chapter 2: Registering and Downloading . . . . . . . . . . . . . . . . . . . . . . . .33
Registering with RIM 33
Signing Up to Be a Citizen of the App World 37
Getting the JDE 42
Which JDE version is right for you? 44
Playing it safe 46
02_467114-ftoc.indd vii02_467114-ftoc.indd vii 8/30/10 1:03 PM8/30/10 1:03 PM
www.it-ebooks.info
BlackBerry Application Development For Dummies
viii
Chapter 5: Setting Up Screens and User Interfaces . . . . . . . . . . . . . .109
The Screen 110
Choosing a screen type 110
Creating a screen 111
Extending a screen’s basic functionality 114
User Interface Elements 115
User interface callbacks 117
Creating a custom user interface element 118
Menus 119
Understanding the MenuItem class 120
Creating a menu in a BlackBerry application 121
Responding to User Interaction 123
02_467114-ftoc.indd viii02_467114-ftoc.indd viii 8/30/10 1:03 PM8/30/10 1:03 PM
www.it-ebooks.info
ix
Table of Contents
Laying Out the User Interface 124
Controlling the layout of the user interface 124
Creating a screen with a custom layout manager 126
Threaded Operation 129
Understanding when to use threads 130
Using a thread to update the display from the background 131
The Screen Stack 135
Chapter 6: Storing Your Users’ Data . . . . . . . . . . . . . . . . . . . . . . . . . . . .137
Understanding BlackBerry Storage Models 138
Persistent Storage 138
Persistent storage methods 139
Kinds of information to store in persistent storage 142
How persistent is persistent storage? 142
Runtime Storage 143
x
Interacting with E-Mail 203
Handling attachments 204
Writing an e-mail attachment handler 207
Standard BES IT Policies 210
Reading Application IT Policies 212
Dealing with BES Security 213
Chapter 9: Networking Your BlackBerry App. . . . . . . . . . . . . . . . . . . .215
Using a Well-Connected BlackBerry 216
Checking for service 217
Choosing what service to use 218
Communicating with Services on the Internet 219
Structuring your data 220
Behaving like a well-mannered application 222
Coding to send data to a network service 224
Setting Up a Push Listener 234
Part IV: Finishing and Debugging Your App 241
Chapter 10: Running Your Code on a Real Device . . . . . . . . . . . . . . . .243
Moving from Simulator to Device 244
Signing up to do signing 244
Signing apps on multiple machines 246
The Build Process, Revisited 247
Signing Your Application 247
Understanding what the signing process does 250
Finding out if your signing succeeded or failed 251
Deploying Your Application onto a Real Device
Using Desktop Manager 252
Running Your Application 256
Chapter 11: Debugging Your Application. . . . . . . . . . . . . . . . . . . . . . . .257
Understanding Where Errors Occur 257
Operating in the background 312
Streamlining Your App 315
Don’t reinvent the wheel 315
Group source les using the package hierarchy 316
Keep method sizes small 316
Each class should accomplish just one purpose 317
Reduce the public methods in your
classes to the bare minimum 317
Backing Up and Organizing Your Code 317
Backing up your precious data 317
Keeping your code organized with
a source code control system 319
Chapter 14: Application Deployment and Upgrades . . . . . . . . . . . . . .321
Delivery from a Desktop PC 322
Delivery through a BES 324
Delivery via the Web 325
Upgrading Your App 327
Insistent persistence 329
Handling multiple versions of your app 334
02_467114-ftoc.indd xi02_467114-ftoc.indd xi 8/30/10 1:03 PM8/30/10 1:03 PM
www.it-ebooks.info
BlackBerry Application Development For Dummies
xii
Part VI: The Part of Tens 337
Chapter 15: Ten Most Useful Sample BlackBerry Apps . . . . . . . . . . .339
contactsdemo 339
custombuttonsdemo 340
httpdemo 340
gpsdemo 340
localizationdemo 340
T
he advent and growing popularity of BlackBerry smartphones has
changed how corporate users communicate whenever away from their
offices. No longer tied to their landline phones, no longer glued to their
desktop PCs, corporate users could stay in touch via voice and e-mail as
long as cellphone reception was available. Then, Research In Motion (RIM)
upped the ante: Independent software developers were allowed to create
software to run on BlackBerry smartphones. Developers familiar with Java
(the BlackBerry uses the Java programming language) could leverage that
knowledge to create BlackBerry apps. This opened the BlackBerry smartphone
to the creative power of developers all across the world.
In April 2009, RIM went one step further: The BlackBerry App World was
introduced, offering developers a place to market, advertise, and sell their
applications to all BlackBerry users.
BlackBerry Application Development For Dummies shows you how to develop
an application from concept to completion, from coding to uploading it to the
BlackBerry App World to sell it to BlackBerry users.
About This Book
BlackBerry Application Development For Dummies is a guide to developing
BlackBerry smartphone applications. No BlackBerry development experience
is required, but familiarity with the Java programming language is assumed.
After all, Java is the language you use to develop applications for BlackBerry
smartphones, and all the API documentation follows the Java documentation
guidelines as well as providing coding examples in Java.
The BlackBerry platform enables and encourages you to create minimalist
applications that can do some pretty powerful and useful things. You can
start small, making simple apps that do a few really important things for your
users, and then over time, improve and increase the features and functionality
that your apps deliver.
03_467114-intro.indd 103_467114-intro.indd 1 8/30/10 1:03 PM8/30/10 1:03 PM
The challenge I faced when I started coding
for the BlackBerry was that I had nothing
to show me the all-important how and why.
The application programming interface (API)
documentation would tell me what each object
did, but gave no instruction for how to coordinate
the actions and interactions of the scores of
objects that make up a BlackBerry application.
The sample applications were ready to go but
provided no reasons for why a particular coding
choice was made. Those apps are useful to see
how a particular feature can be implemented,
but there is no “guiding philosophy” shining
over the entire set.
All through my early BlackBerry development,
I searched for a book that would show me the
whys and hows to get my applications working
right. I didn’t find anything. Eventually, I decided
it was time to write the book I had been searching
for — BlackBerry Application Development For
Dummies.
03_467114-intro.indd 203_467114-intro.indd 2 8/30/10 1:03 PM8/30/10 1:03 PM
www.it-ebooks.info
3
Introduction
Java code is case sensitive, so when you use code that appears in this book,
type it exactly as it appears. (You can find code samples for this book at www.
dummies.com/go/blackberryappdev — download the code samples, and
you won’t have to type long code blocks!)
4
BlackBerry Application Development For Dummies
And finally, I assume you have some programming knowledge and that you
have at least a basic understanding of object-oriented programming (OOP),
specifically in Java. If you’re not up to speed with Java, consider Java For
Dummies, 4th Edition, by Barry Burd, or Java All-In-One Desk Reference
For Dummies, 2nd Edition, by Doug Lowe and Barry Burd (all from Wiley
Publishing). Sun’s online tutorials are helpful as well.
How This Book Is Organized
The chapters in BlackBerry Application Development For Dummies are divided
into seven parts.
Part I: Getting Started
on BlackBerry Apps
Part I takes you into the world of BlackBerry application development. You
find out about BlackBerry applications in general, and you discover some of
the challenges that your app might encounter on a BlackBerry smartphone.
You also discover how to become a registered BlackBerry developer and all
the steps you need to take so you’re ready to deliver to the BlackBerry App
World.
Part II: BlackBerry Application
Development
In Part II, you dive right into code. Not the deep end, but not exactly shallow,
either. You start with structure and then touch all the pieces of code to make
an application do everything it needs to do to communicate with the user
and behave like a proper BlackBerry app.
Part III: Developing Enterprise-Class
BlackBerry Apps
In Part III, I expose you to the use of BlackBerry devices in the world of a
corporate enterprise, which is where many BlackBerry users live. You discover
the advantages of a BlackBerry that is tied directly to a corporate network —
developing your application. The second appendix gives you information
about real devices and how to use them to test your application. Some of the
real services are a bit beyond the beginner level for setting up, but Appendix
B provides you with the information you need to be aware of when your
application encounters them in the real world.
Icons Used in This Book
Like all For Dummies books, this book makes frequent use of icons to help
identify important, helpful, or technical information. Take heed when you see
one of the following icons.
03_467114-intro.indd 503_467114-intro.indd 5 8/30/10 1:03 PM8/30/10 1:03 PM
www.it-ebooks.info
6
BlackBerry Application Development For Dummies
This icon indicates a useful pointer that you shouldn’t skip. Tips make your
coding life easier by showing a shortcut or letting you know the information
next to it shows you the easiest approach to a coding problem.
This icon represents a friendly reminder. It describes a vital point that you
should keep in mind while proceeding through a particular section of the
chapter.
This icon signifies that the accompanying explanation might be informative,
maybe even interesting, but is technical and isn’t required for your goal of
understanding BlackBerry application development. Feel free to jump over
these little pieces.
This icon alerts you to potential challenges you may encounter on the way.
Read and obey these commentaries to avoid problems down the road.
Where to Go from Here
You’re ready to begin the BlackBerry adventure. You can, of course, turn the
page and continue reading at Chapter 1. If you haven’t registered with RIM
to become a BlackBerry application developer and downloaded the JDE, I
recommend you hop right to Chapter 2. If you have a particular question or
Vendor, which allows you to deliver your application to
RIM for review as a submission to the BlackBerry App
World.
Also in this part, you get a chance to see what’s already in
the App World, which presents an opportunity to improve
and refine your app idea. Finally, you get your feet wet by
producing a simple application that will run on a simulator
or a real device.
04_467114-pp01.indd 804_467114-pp01.indd 8 8/30/10 1:03 PM8/30/10 1:03 PM
www.it-ebooks.info
Chapter 1
Gathering What You Need to
Develop BlackBerry Apps
In This Chapter
▶ Discovering BlackBerry apps and why to develop them
▶ Collecting the right tools
▶ Sharpening the right skills
▶ Meeting the challenges of BlackBerry development
A
BlackBerry application is meant to be small, fast, and responsive to
its users. BlackBerry smartphones are small, fast, and function as
mini-communications centers: a phone, text-messaging system, e-mail client,
and Web browser. Your app should give the user the same kind of experience
as the standard apps that come with the BlackBerry, providing information
quickly and easily with a minimal amount of input.
In this chapter, I show you what tools, skills, and ideas you need to gather
and discover to start developing BlackBerry apps.
Why Develop BlackBerry Apps?
The BlackBerry App World from Research In Motion (RIM) provides a mar-
ketplace devoted to BlackBerry users, and a great many apps of all different
develop and maintain, and they don’t require a large development team:
You need fewer people to debate the pros and cons of different ways to
do the same thing.
✓ BlackBerry apps narrowly focus on delivering what the user wants,
and no more. The apps are simple and direct, providing the user with
only the information they want — and the tools to get it.
✓ BlackBerry apps use Java. You can leverage any desktop PC Java
programming experience you have.
✓ The tools and simulators are all free. You can do all your development
on a Windows PC. The simulators all execute the same code as the
05_467114-ch01.indd 1005_467114-ch01.indd 10 8/30/10 1:05 PM8/30/10 1:05 PM
www.it-ebooks.info
11
Chapter 1: Gathering What You Need to Develop BlackBerry Apps
actual devices, so you can be sure that if your app works on a simulator,
it will work on a real device.
✓ The BlackBerry is widely used in corporate enterprises. From CEOs to
administrative assistants and everyone in between, you have a market
for business-specific apps that could link everyone in the enterprise.
✓ RIM provides the BlackBerry App World for you to showcase and sell
your app. This venue removes the responsibility of credit card handling,
hosting, downloading, and notifying users of updates. The App World
comes with a variety of pricing tiers, including free and Try & Buy. RIM
keeps 20 percent of your application price to cover some of its costs.
Submitting your app to the App World incurs a $20 fee per submission,
which you can buy in blocks of ten for $200.
Discovering Apps, BlackBerry Style
BlackBerry users are on-the-go, fast-paced, living in the moment, and your
app will need to behave accordingly. BlackBerry users are interested in getting