You are on page 1of 11

Developing Android Applications with Adobe AIR

by Vronique Brossier
Copyright 2011 Vronique Brossier. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.

Editor: Mary Treseler


Production Editor: Kristen Borg
Copyeditor: Audrey Doyle
Proofreader: Kristen Borg

Indexer: John Bickelhaupt


Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Robert Romano

Printing History:
May 2011:

First Edition.

Nutshell Handbook, the Nutshell Handbook logo, and the OReilly logo are registered trademarks of
OReilly Media, Inc. Developing Android Applications with Adobe AIR, the image of a Royal Flycatcher,
and related trade dress are trademarks of OReilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and OReilly Media, Inc., was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.

ISBN: 978-1-449-39482-0
[LSI]
1303389007

Table of Contents

Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
1. AIR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Installing the Adobe Development Tools
Flash Professional CS5.5
Flash Builder 4.5
Installing the AIR Runtime on an Android Device
What Is in the AIR SDK
New ActionScript Libraries
Functionalities Not Yet Supported
AIR on the Desktop Versus AIR on Android
Mobile Flash Player 10.1 Versus AIR 2.6 on Android

2
2
2
2
3
4
5
5
6

2. Call Me, Text Me . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7


Setting Up Your Device
Creating a Project
Using Flash Professional
Using Flash Builder
Creating the Application Descriptor
Using Flash Professional
Using Flash Builder
Writing the Code
Using Flash Professional
Using Flash Builder
Packaging Your Application As an APK File and Installing
It on the Device
Using Flash Professional
Using Flash Builder
Testing and Debugging

8
8
8
9
9
9
9
10
11
12
12
12
13
13

vii

Using Flash Professional


Using Flash Builder
Mobile Utility Applications
Launchpad
Device Central CS5
Package Assistant Pro
De MonsterDebugger
Installing AIR on an Android Device via a Server
Other Tools
Conclusion

13
14
15
15
15
15
16
16
16
18

3. Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Android Software Development Kit
Installing the Android SDK
Installing the Android Debug Bridge
Detecting Your Device
Using the Dalvik Debug Monitor
Using the logcat Command
Using the Virtual Device Manager and Emulator
How Does AIR Run on Android?
Starting AIR with intent
AIR Access to Android APIs
Using the Command-Line Tool
A Basic Review
Conclusion

20
20
21
22
22
23
24
25
25
26
27
27
28

4. Permissions, Certificates, and Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29


Why Mobile?
The APK File
Creating the Application Icon
Choosing the Application Settings
Setting Permissions
Packaging External Resources
Signing Your Application with a Certificate
Versioning
Registering As an Android Developer
Publishing an Application on the Android Market
Uploading Assets
Listing Details
Publishing Options
Distributing Applications via Adobe InMarket
Publishing for the Amazon Market
Controlling Distribution by Using the MAC Address
viii | Table of Contents

29
30
30
31
33
36
36
37
38
38
38
38
39
39
40
40

Launching an AIR Application


Monetizing Your Application
Paid Applications
Mobile Ads
Reporting
Conclusion

41
41
41
41
42
43

5. Evaluating Device Capabilities and Handling Multiple Devices . . . . . . . . . . . . . . . . . 45


Hardware
The Processor
Memory and Storage
The Camera
Sensors
The Battery
The Display
Software
Performance
Capabilities
Orientation
Creating Content for Multiple Screens
Asset Scaling and Positioning
Vector Graphics or Bitmaps?
Developing a Deployment Strategy
Considering Connectivity
Conclusion

45
46
46
46
46
46
47
47
48
48
49
50
51
54
54
55
55

6. Opening and Closing an Application and Saving Data . . . . . . . . . . . . . . . . . . . . . . . . 57


The AIR Application
Opening the Application
Closing the Application
Moving Between the Background and Foreground
Setting Back, Menu, and Search Buttons
Overriding a Dimmed Screen
Why and How to Save Data
Internal or External Storage?
Local SharedObject
The Filesystem
Using the SQLite Database
Embedding a Database
Using Encrypted Local Storage
Conclusion

58
58
58
59
61
61
62
63
65
66
70
76
77
77

Table of Contents | ix

7. Multitouch Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
A Brief History
What Is Multitouch and Gesture?
How Does It Work?
The Multitouch Class
The GestureEvent Class
The Zoom Gesture
The Rotate Gesture
The Pan Gesture
The Swipe Gesture
The Press and Tap Gesture
The Two-Finger Tap Gesture
The TouchEvent Class
The GestureWorks Library
Designing for Touch
Conclusion

79
80
80
81
82
82
83
85
86
87
88
88
91
91
92

8. Accelerometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
What Is a Motion Sensor?
The Accelerometer Class
Visualizing the Values
A Simple Animation
Updates and Screen Rendering
Setting Boundaries
Rotating Toward the Center
Shake Me
Smoothing Out Values
Conclusion

93
93
95
95
96
97
98
99
100
102

9. Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
The Gallery Application and the CameraRoll Class
Selecting an Image
Adding an Image
The Camera Application and the CameraUI Class
Uploading to a Remote Server
EXIF Data
Conclusion

103
104
109
109
111
111
114

10. Geolocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115


Geolocation Classes
The GeolocationEvent Class
Locating a Device Using Global Positioning System and Network/WiFi
Technology
x | Table of Contents

115
117
118

Using GPS
Using the Cellular Network and WiFi
How to Know if GPS or WiFi Is Active
AIR and Android
Reverse Geocoding
Maps
Launching Google Maps
Static Maps
Dynamic Maps
EXIF Data and the Map Object
The speed Property
Conclusion

119
119
120
121
122
124
124
125
129
132
134
134

11. Microphone and Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137


The Microphone
Recording Audio
Playing Back Audio
Saving a Recording
Audio Assets
Embedding Files
Using External Files
Settings and the Audio Codec
Working with Sounds
Loading Sounds
Playing Sounds
Displaying Progress
Stopping Sounds
Resuming Sounds
Accessing Metadata
Audio Example Using Multitouch
ID3 Tags
Modifying Sound
Controlling Volume
Panning
Raw Data and the Sound Spectrum
Audio and Application Activity
Conclusion

137
138
139
140
144
144
144
145
147
147
149
149
150
151
151
152
152
152
152
154
154
158
158

12. Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161


Preparing Video
Codecs
Bit Rate
Frame Rate

161
161
163
163
Table of Contents | xi

Resolution
Performance
Playing Video
Embedded Video
External Video
Progressive Video
RTMP Streaming
HTTP Dynamic Streaming
Peer-to-Peer Communication
Controls
YouTube
Capturing Video
Video and the CameraUI Class
The Camera Class
Documentation and Tutorials
Conclusion

163
164
164
165
165
165
169
171
171
171
172
172
172
175
176
176

13. StageWebView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177


The Native Browser
The StageWebView Class
Design Considerations
Local Use
Mobile Ads
Services and Authentication
Limitations
Conclusion

177
178
180
181
182
184
185
185

14. Hardware Acceleration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187


Some Definitions
Rendering, or How Things Are Drawn to the Screen
Computation
Edge and Color Creation
Rasterization
Presentation
GPU Rendering on Android
The cacheAsBitmap Property
The cacheAsBitmapMatrix Property
The Display List
Memory Consumption
Tree Structure
Node Relationship
MovieClip with Multiple Frames
Interactivity
xii | Table of Contents

187
188
189
189
189
190
190
190
193
195
195
196
196
198
198

Multiple Rendering Techniques


Maximum Texture Memory and Texture Size
2.5D Objects
How to Test the Efficiency of GPU Rendering
Matrices
Identity Matrix
Transformation Matrix
Matrices for Multiscreen Deployment
Matrices Not to Be Used in GPU Mode
Hardware-Accelerated Audio and Video
Conclusion

199
199
199
200
200
200
201
202
202
203
203

15. Your Device and Others . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205


RTMFP UDP
P2P Over a Local Network
Color Exchange
Companion AIR Application
P2P Over a Remote Network
Simple Text Chat
Multicast Streaming
End-to-End Stream
Directed Routing
Relay
Treasure Hunt
Other Multiuser Services
Arduino and Physical Computing
Conclusion

205
206
208
210
210
212
213
215
217
218
219
219
220
220

16. ViewManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221


Navigation
ViewManager
Individual Views
Breadcrumb Navigation
Flash Builder ViewNavigator
Conclusion

221
221
225
232
234
237

17. Case Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239


The Album Application
Design
Architecture
Flow
Permissions
Navigation

239
240
240
240
243
244
Table of Contents | xiii

Images
Audio
Reverse Geolocation
SQLite
P2P Connection
Scrolling Navigation
Desktop Functionality
Conclusion

244
244
244
244
245
245
248
249

18. Asset Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251


Text
The Virtual Keyboard
Fonts
The Flash Text Engine
Optimizing Art
Bitmap Size and Mip Mapping
Vector Graphics at Runtime
Scaling
cacheAsBitmap
cacheAsBitmapMatrix
Vector to Bitmap
Compositing Vector Graphics
MovieClip with Multiple Frames
Sprite Sheet and Blitting
Blitting
Custom Cursor
Asynchronous Bitmap Decoding
Caching Assets
Components
Conclusion

251
251
252
252
254
254
255
255
255
255
256
256
257
259
259
261
261
261
262
263

19. Best Practices for Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265


Resources on Optimization
Where to Find Help
Documentation
The Internet
The Community
How Does It Run?
The Concept of Frame
Memory
Creating Objects
Removing Objects
Garbage Collection
xiv | Table of Contents

265
266
266
266
267
267
267
269
270
270
272

Events
Event Propagation
One Listener for Many Children
Generic and Custom Events
Diagnostics Tools
Hi-Res-Stats
Flash Builder Profiler
Flash Preload Profiler
Grant Skinners PerformanceTest
Native Tools
Conclusion

273
274
275
276
277
277
278
279
279
279
279

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

Table of Contents | xv

You might also like