Android Development with Flash - Julian Dolce - E-Book

Android Development with Flash E-Book

Julian Dolce

3,8
22,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

The visual guide to developing for one of the world's hottest new mobile platforms, the Android OS The Android operating system works on phones that combine a camera, Web browser, e-mail, GPS, and mapping tool into a single accessible pocket-sized unit, and can function on computers, as well. Aimed at visual learners and packed with hundreds of screen shots, this guide brings Flash developers up to speed on the necessary factors to take into account when developing for this touch-based, mobile platform. Experienced Flash developer Julian Dolce escorts you through the process of creating applications for the Android OS using the Flash CS5 development platform and informs you of best practices to try as well as common pitfalls to avoid. * Guides you step by step through the process of creating applications for the Android OS using Flash CS5 * Explores the capabilities and limitations of developing apps for the Android OS * Points out common pitfalls and teaches you best practices * Features hundreds of screen shots to assist with visual learning Android Development with Flash: Your visual blueprint for developing mobile apps gets you on your way to developing apps for Android... in a flash!

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 431

Veröffentlichungsjahr: 2011

Bewertungen
3,8 (16 Bewertungen)
7
2
4
3
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Android™ Development with Flash®

Table of Contents

Chapter 1: Getting Started with Android Development

Introducing Android Devices

Introducing the Development Tools

Introducing the Available APIs

Check What APIs Are Not Available

Become an Android Developer

Get the Android SDK

Get the Android Eclipse Plug-in

Enable USB Debugging

Create an Android Virtual Device

Start the Emulator

Chapter 2: Getting Started with Flash CS5

Using the Actions Panel

Create a Skeleton Custom Class

Set the Source Path

Create MovieClips

Create Buttons

Edit Properties in Flash

Add Objects to the Stage with Code

Remove Objects from the Stage with Code

Work with Events

Using the Drawing API

Using Flash CS5 Help

Chapter 3: Developing Your First Application

Create a New Project

Configure Publish Settings

Set Your Application Output

Create a P12 Certificate

Compile from Flash Professional CS5

Compile from the Command Line

Install Your Application on Your Device

Update Your Version Number

Set Application Permissions

Set a Custom Application URI

Chapter 4: Designing Your Application

Mobile User Interface Guidelines

Understanding Screen Resolutions

Create Full-Screen Applications

Understanding Screen Orientation

Create Usable Hit States

Understanding Layout

Chapter 5: Handling Interaction

Create Button States

Respond to Touch Events

Track Multiple Touches

Respond to Zoom Events

Respond to Rotate Events

Respond to Pan Events

Respond to Swipe Events

Listen for Accelerometer Events

Determine If the Accelerometer Is Available

Determine Device Orientation

Detect Which Way Is Up

Filter Accelerometer Data

Chapter 6: Working with Images

Prepare Your Images

Import Images

Display Images

Bundle Images with Your Application

Load Images at Runtime

Create Images Dynamically

Save Images to the Camera Roll

Select Images from the Camera Roll

Display the Camera

Chapter 7: Working with Sound

Import Audio into Your Project

Choose an Audio Codec

Bundle Sounds with Your Application

Load Sounds at Runtime

Play Sounds

Stop Sounds

Set the Volume of a Sound

Visualize the Sound Spectrum

Access the Microphone

Chapter 8: Working with Video

Explore Available Video Formats and Encode a Video File

Convert Videos

Embed a Video

Bundle a Video with Your Application

Load a Video

Buffer a Video

Control a Video

Set the Volume of a Video

Chapter 9: Working with Text

Embed Fonts in Your Application

Create an Input TextField

Create a Password TextField

Using TLF TextFields

Create a Scrollable TextField

Chapter 10: Saving State

Create a Local SharedObject

Write to a SharedObject

Load Data from a SharedObject

Connect to a SQLite Database

Create a SQLite Table

Insert Data into a SQLite Table

Select Data from a SQLite Table

Update Data in a SQLite Table

Delete Data from a SQLite Table

Handle Application Exits

Save Application States

Handle Back and Menu Button Presses

Handle Application Deactivation

Chapter 11: Working with Files

Reference Files and Directories

Write Files

Read Files

Update Files

Append Files

Handle Files Synchronously

Copy Files

Load SWF files

Chapter 12: Using the Location and WiFi Features

Retrieve Your Current Location

Map Your Location with Yahoo!

Map Your Location with Google

Determine Your Speed

Check for an Internet Connection

Set the System Idle Mode

Display Web Pages

Chapter 13: Using Special URL Protocols

Make Phone Calls

Open the Mail Application

Open the Maps Application

Open the Messaging Application

Play a YouTube Video

Chapter 14: Integrating with Third-Party Services

Submit Updates to Twitter

Display Ads with Smaato

Track with Google Analytics

Display Ads with AdMob

Chapter 15: Optimizing Performance

Optimize Your Display List

Manage Mouse Events

Understanding cacheAsBitmap

Understanding cacheAsBitmapMatrix

Chapter 16: Debugging Your Application

Show Your Trace Statements

Create Breakpoints

Using the Flash CS5 Debugger

Understanding the Debug Console

Understanding the Variables Panel

Debug with the Android Eclipse Plug-in

Chapter 17: Deploying Your Application

Take Screenshots of Your Application on Your Device

Create an Application Icon

Publish Your Application for the Android Market

Upload Your Application to the Android Market

Appendix A: ActionScript Class Reference

Android™ Development with Flash®

Your visual blueprint™ for developing mobile apps

By Julian Dolce

Android™ Development with Flash®: Your visual blueprint™ for developing mobile apps

Published by Wiley Publishing, Inc.10475 Crosspoint BoulevardIndianapolis, IN 46256

www.wiley.com

Published simultaneously in Canada

Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana

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 permitted 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 www.wiley.com/go/permissions.

Library of Congress Control Number: 2010934750

ISBN: 978-1-118-03635-8

Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1

Trademark Acknowledgments

Wiley, the Wiley Publishing logo, Visual, the Visual logo, Visual Blueprint, Read Less - Learn More, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates. Android is a trademark of Google Inc. Use of this trademark is subject to Google Permissions. Adobe and Flash are either registered trademarks or trademarks of Adobe Systems Incorporated in the United States and/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc. is not associated with any product or vendor mentioned in this book.

LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.

FOR PURPOSES OF ILLUSTRATING THE CONCEPTS AND TECHNIQUES DESCRIBED IN THIS BOOK, THE AUTHOR HAS CREATED VARIOUS NAMES, COMPANY NAMES, MAILING, E-MAIL AND INTERNET ADDRESSES, PHONE AND FAX NUMBERS AND SIMILAR INFORMATION, ALL OF WHICH ARE FICTITIOUS. ANY RESEMBLANCE OF THESE FICTITIOUS NAMES, ADDRESSES, PHONE AND FAX NUMBERS AND SIMILAR INFORMATION TO ANY ACTUAL PERSON, COMPANY AND/OR ORGANIZATION IS UNINTENTIONAL AND PURELY COINCIDENTAL.

Contact Us

For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993 or fax 317-572-4002.

For technical support, please visit www.wiley.com/techsupport.

The Great Wall of China

The Great Wall of China is the world’s longest structure built by human hands, stretching more than 4,000 miles. Begun during the 5th century B.C., construction on the Great Wall of China continued into the 16th century. Building materials were often determined by the surrounding topography and include packed earth, stones, bricks, tiles, and quarried limestone. Like a dragon, the Wall winds through mountains, grasslands, deserts, and plateaus — and through the heart of Chinese history and culture.

Learn more about the Great Wall and other marvels of China in Frommer’s China, 4th Edition (ISBN 978-0-470-52658-3), available wherever books are sold or at www.Frommers.com.

Sales

Contact Wileyat (877) 762-2974or (317) 572-4002.

Credits

Acquisitions Editor

Aaron Black

Project Editor

Dana Rhodes Lesh

Technical Editor

Paul Geyer

Copy Editor

Dana Rhodes Lesh

Editorial Director

Robyn Siesky

Editorial Manager

Rosemarie Graham

Business Manager

Amy Knies

Senior Marketing Manager

Sandy Smith

Vice President and Executive Group Publisher

Richard Swadley

Vice President and Executive Publisher

Barry Pruett

Project Coordinator

Sheree Montgomery

Graphics and Production Specialists

Carrie A. CesaviceAndrea HornbergerJennifer Mayberry

Quality Control Technicians

Rebecca DenoncourLindsay Littrell

Proofreader

Mildred Rosenzweig

Indexer

Valerie Haynes Perry

Media Development Project Manager

Laura Moss

Media Development Assistant Project Manager

Jenny Swisher

Media Development Associate Producers

Josh FrankMarilyn HummelDoug KuhnShawn Patrick

Screen Artists

Ana CarilloCheryl GrubbsJill A. ProllRonald Terry

Cover Art Illustrator

Cheryl Grubbs

About the Author

Julian Dolce is the senior Flash developer at QNX Software Systems, specializing in mobile AIR applications. Julian has spoken at numerous conferences around the world, where he has taught workshops on moving from Flash development to iPhone development, as well as a number of AIR for Android development workshops. He also maintains a personal development blog, www.deleteaso.com, in which he writes about his life as a Flash developer.

Author’s Acknowledgments

This book would not have been possible without the support of my close friends, Thomas, Elissa, Miles, Marco, Lori, Neil, and Sherisse. You guys put up with me for six months and made sure that I did not go insane. You are the best friends anyone could ask for.

Erin, your love, support and encouragement throughout my entire career has been a source of strength and inspiration. Your courage and determination has taught me that any dream, no matter how big, is never out of reach. You never stopped believing in me, and I cannot thank you enough for it.

Finally, to my parents and the rest of my family. You have supported me throughout my entire life, and I could not have gotten to where I am today without you.

How to Use This Visual Blueprint Book

Who This Book Is For

This book is for intermediate-to-advanced Flash developers who want to use their knowledge of Flash and ActionScript to develop AIR Android applications. This book specifically focuses on the Android platform, but many of the topics and examples can be used to develop for any AIR mobile platform.

The Conventions in This Book

Steps

This book uses a step-by-step format to guide you easily through each task. Numbered steps are actions you must do; bulleted steps clarify a point, step, or optional feature; and indented steps give you the result.

Notes

Notes give additional information — special conditions that may occur during an operation, a situation that you want to avoid, or a cross-reference to a related area of the book.

Extra or Apply It

An Extra section provides additional information about the preceding task — insider information and tips for ease and efficiency. An Apply It section takes the code from the preceding task one step further and allows you to take full advantage of it.

Bold

Bold type shows text or numbers you must type.

Courier Font

Courierfont indicates the use of scripting language code such as statements, operators, or functions, and code such as objects, methods, or properties.

• Italics

Italic type introduces and defines a new term.

Web Site

You can find the code samples throughout the book on the Wiley Web site, www.wiley.com. Search the site for this book’s page; then you can find the code samples on the Downloads tab.

Chapter 1: Getting Started with Android Development

Introducing Android Devices

It is an exciting time to be a Flash developer. Adobe has taken big steps in making the Flash platform available on as many devices as possible. The Open Screen Project is an Adobe-led initiative whose goal is to “enable consumers to engage with rich Internet experiences seamlessly across any device, anywhere,” as it says on its Web site, at www.openscreenproject.org. Flash Player 10.1 will be available for multiple mobile platforms, such as Google Android, RIM’s BlackBerry, Palm Pre, and Nokia, as well as numerous other devices such as TVs, set top boxes, tablets, and netbooks. Adobe is working with these and over 50 other partners to optimize Flash Player 10.1 in order to work better with the different devices.

Flash Player 10.1

There are two very different parts to the Flash platform on mobile devices. Flash Player 10.1 has been optimized for use in mobile browsers. The intent is to have all the content that you would normally see on a desktop browser work just as you would expect on a mobile device. Flash Player 10.1 also takes advantage of some of the new mobile device APIs, such as multitouch and geolocation. If you currently have Flash content on the Web, it is a good idea to test it on an Android device with Flash Player 10.1 installed. Consider updating the content to better support mobile devices if possible. As mobile devices become more popular, users will demand that content work seamlessly across all platforms. The Flash Player 10.1 plug-in is available now for supported Android 2.2 devices in the Android Market.

AIR for Mobile Devices

The other piece of the Flash platform mobile story is AIR for mobile devices. AIR (Adobe Integrated Runtime) was originally designed and developed for the desktop, and some features do not translate well to mobile platforms. To help with this, Adobe has created the mobile profile for AIR, which is a subset set of AIR. To find out which APIs are available, see the sections “Introducing the Available APIs” and “Check What APIs Are Not Available” later in this chapter.

Currently, the AIR mobile profile is available for iPhone OS and Android devices. However, because of the new terms of use by Apple, you can no longer use Flash CS5 to create and submit applications to the Apple iTunes App Store. The AIR Runtime is available on Android devices and needs to be installed on the device in order to run any AIR applications. Similarly to the desktop runtime, the mobile runtime will be installed if the user tries to install an AIR for Android application without the runtime.

The AIR Runtime is currently supported on Android devices with Android 2.2, also known as Froyo, installed or higher. It also is currently supported only on Android devices with an ARMv7 processor. Most new devices should support the AIR Runtime; however, you will want to double check before you purchase a device.

Because the Android platform is open source, many device manufacturers ship their devices with custom Android skins. As Google releases new versions of Android, each manufacturer must convert its skins to the new version before the device can be updated. Because of this, it is a good idea to stay away from devices that have highly customized skins, such as the Sony Ericsson Xperia X10, which is capable of running AIR except that it currently ships with Android 1.6.

Android AIR Devices

The following are a few of the devices that are currently available that support the AIR Runtime and Android AIR applications. You may still need to update them to Android 2.2, but after they are updated, they should run all applications.

Google Nexus One

The Nexus One is manufactured by HTC for Google and can be purchased from Google unlocked. Being able to purchase an unlocked device is attractive for developers, as they are not restricted to a specific network provider. This also means that you can purchase one if you plan to use it only as a development device and not as an everyday mobile phone. There are two versions available that support the different cell network providers and can be purchased for the United States, Canada, and Europe.

The Nexus One ships with Android 2.1, a 1GHz Snapdragon processor, and 512MB of memory. It also has 512MB of Flash memory storage and a 4GB microSD card. The SD card can be upgraded to a 32GB card. The Nexus One has a screen resolution of 480 x 800 pixels and has a 5.0 megapixel camera, which can shoot 720 x 480 video at 20fps or higher.

Motorola Droid

Outside of the United States, the Motorola Droid is referred to as the Motorola Milestone. Both devices currently support Android 2.1. There are two big differences between the Droid and the Nexus One: The Droid has a slide-out keyboard and a screen resolution of 480 x 854 pixels.

HTC Desire

The HTC Desire is one of the more popular Android phones on the market today, and it is very similar to the Nexus One. The biggest difference between the two devices is the Desire’s HTC Sense Android user interface. The Desire also has tactile buttons across the bottom as opposed to the Nexus One’s touch-sensitive buttons.

Android Tablets

There are a number of Android tablet devices that are set to release in the last half of 2010, and there are too many to discuss here. However, the most important thing to understand is the difference in screen resolution. 1024 x 768, 1366 x 768, 800 x 600, and 1024 x 600 are just some of the different screen resolutions for tablets that will support AIR Android applications. Testing how your application will look and respond on these different screen resolutions will ensure that users have a good user experience across devices.

Test for Multiple Devices

As you can see, there are many differences between all the different models, and the tablets are a game changer. There are lots of tips and things to think about in this book when developing your applications to support multiple platforms. It is a good exercise to try and take all of these into consideration early on in development. Some things to ask yourself are, “What does my application look like in multiple resolutions?” and “How does a user interact with my application on a non–touch-enabled device?.” Even if you ever plan to support only one device today, allowing for multiple platforms in the future will prove to be worthwhile. The iPad is a great example of developers never planning for a different resolution or platform. When the iPhone SDK first came out, developers were fortunate and had to design for only one screen size and resolution. Now with the release of the iPad and the fourth generation iPhone, developers have to redesign and even rewrite their applications in order to support these new platforms.

Introducing the Development Tools

If you have developed Flash applications before, you will already be familiar with some of the tools that will be explored throughout this book. However, some of them may be new to you. The following are all the different tools that are discussed throughout the book.

Flash CS5 Professional

Flash CS5 is the main integrated development environment (IDE) for developing Flash applications for the Web, desktop, and Flash Lite–enabled mobile devices. In this 11th version of Flash, Flash CS5 introduced us to the ability to publish Flash applications to native iPhone applications, which ships with the product. In the fall of 2010, Adobe released an update for Flash CS5 and AIR, which enables you to develop and publish applications for Android devices. The update can be downloaded through the Adobe CS5 Updater application or from the Adobe Web site. If you are not sure if you have the latest version, select Updates from the Help menu in Flash CS5.

Flash Builder

Flash Builder, formerly know as Flex Builder, is an Eclipse-based IDE for creating Flex and AS3 projects. Flash Builder is Adobe’s main ActionScript coding application. Flash CS5 does have the capability to write separate ActionScript code and classes; however, Flash Builder provides a much more feature-rich development environment. With the newest version of Flash Builder, Adobe has also integrated a better workflow between it and Flash CS5. You are now able to publish .fla files directly from Flash Builder without having to switch between applications. One of the benefits of Flash Builder is that it is built on top of Eclipse, a popular open source IDE. This enables you to take advantage of the many plug-ins built for Eclipse, which provide additional functionality that you do not get in Flash CS5. There are many plug-ins for managing source control, build integration, and support for other programming languages.

FDT

FDT (Flash Development Tool) is an Eclipse-based IDE similar to Flash Builder. FDT provides many features that speed up development, such as code templates, quick fixes, quick assist, and organized imports — just to name a few. FDT 4 is currently in beta and is quickly becoming one of the most popular ActionScript editors among many of the best Flash developers in the community. FDT 4 gives developers the ability to create their own plug-ins for FDT. This allows community members and developers to create a workflow that is best suited for them. It is only a matter of time before a FDT Android plug-in is released to help with publishing and installing Android applications.

Android SDK

The Android SDK is developed and released by Google, and it enables developers to create native Android applications. There are several tools that come with the Android SDK that are extremely helpful in developing Android applications. Before you begin to develop your application, make sure to download this SDK from the Google Web site. For more details, see the section “Get the Android SDK” later in this chapter.

Android Emulator

The Android emulator comes included in the Android SDK and can be used to test your AIR Android applications on your development computer. The emulator can be configured in a number of different ways, which is extremely useful for testing your application on multiple types of devices. You can also simulate a number of device-specific actions that would normally be available only on a device, such as a voice call, SMS message, and geolocation events.

AIR Runtime for Android

In order for your AIR Android applications to run on an Android device, you must first have the AIR Runtime installed. If an AIR Android application attempts to install without the runtime, it will prompt the user to download the runtime first. This process is very similar to the process of installing an AIR application on the desktop. Having the runtime installed is true for both Android devices and any Android emulators you have created. The AIR Runtime is installed the same way as any other Android application.

Android adb Tool

The Android Debug Bridge (adb) is a tool that comes with the Android SDK. It enables you to manage the state of an Android emulator instance or a connected device. It can be found in the tools folder of the Android SDK and is used by the Flash IDE to install applications onto your connected devices. If you are comfortable with using the command line, you can use adb to drop into a remote shell on an emulator or device instance, and you can issue shell commands on these instances. You can also use the adb tool to push or pull files from your device. This can be extremely useful if you want to better examine a file that your application has created on the device. For more details and other adb commands, check out the adb page on the Android Developers site, http://developer.android.com/guide/developing/tools/adb.html.

Adobe ADT Tool

The AIR Developer Tool (ADT) is used to compile your Android applications from your Flash and AIR applications. The ADT tool can be found in the bin directory of the AIR SDK, as well as the AIK2.5/bin folder in the Adobe Flash CS5 directory. ADT can be used by the command line to package your applications. For more details on compiling Android applications with the command line, see Chapter 3, “Developing Your First Application.” You can also create a self-signed certificate, which can be used to digitally sign your AIR Android applications. It is important to note that applications uploaded to the Android Market must be signed with a certificate that have a validity period ending after October 22, 2033.

Android Market

The Android Market is a place where you can download and install applications to your Android device. It is similar to the iTunes App Store for the iPhone, except it does not have a desktop version and is available only on your device. If you want to submit your application to the Android Market, you need to register with Google for an Android developer account, which costs $25 US. After registration, you will be able to submit applications through the Android Market Web site. For more details on preparing and submitting your applications, see Chapter 17, “Deploying Your Application.” Both the Flash Player 10.1 plug-in for browsers and the AIR Runtime can be downloaded from the Android Market, and you should install them on your device if they are not already.

Introducing the Available APIs

With the ability to publish Android applications from Flash CS5 comes a set of new APIs that enable you to take advantage of some of the features the Android platform has to offer. However, Adobe’s strategy is not to support only the Android platform but as many platforms as possible. This is the reason you may not see as many Android-specific features as you may like or think. Adobe is being very pragmatic about what new features it introduces and how its APIs will look on future platforms, mobile or otherwise. Adobe’s goal is to provide one consistent API for all platforms. For example, the ActionScript code should be the same for accessing a camera whether you are developing applications for the Web, desktop, Android, iPhone, or any other future supported platform.

Accelerometer

The new Accelerometer class, which can be found in the flash.sensors package, gives you the ability to interact with the accelerometer that is built into the device. The Android accelerometer is a three-axis accelerometer capable of measuring both acceleration and gravity. The accelerometer is used to detect the device’s rotation as well as any movements such as shakes.

Geolocation

The flash.sensors.Geolocation class enables you to interact with the device’s location sensor. With this class, you can retrieve the location of your device anywhere in the world. Coordinates are reported to you in the form of latitude and longitude. There are differences in the ways each device figures out your location. It is important to understand that every device that has the Android OS installed does not necessarily have the same location sensor and that accuracy will differ greatly between the devices.

Camera Roll

The flash.media.CameraRoll class enables you to save a BitmapData instance to the device’s camera roll. You can also use the CameraRoll class to enable the user to select an image from the Gallery application on the device.

Stage Orientation

There are a few new classes and methods to help handle stage orientation changes and updates. The flash.display.StageOrientation class defines a set of valid orientations in which the Stage can be set. The flash.display.Stage.setOrienation method, which is new in AIR 2.0, enables you to set the orientation of the Stage based on one of the static properties in the StageOrientation class. And finally, there is a flash.events.StageOrienationEvent class, which allows you to listen for when the Stage orientation is changing and has changed.

Touch Event

The flash.events.TouchEvent class is used to detect when a user touches the screen with his or her finger. The TouchEvent class is very much the alternative to the MouseEvent class but for touches. As well as being available on Android devices, the TouchEvent class is also available on AIR applications in Windows 7 with a touch-enabled screen and Flash CS5 iPhone applications.

Gesture Transform Event

The flash.events.GestureTransformEvent class is used to detect specific user interactions with multiple fingers. Touch-enabled interfaces are still very much in their infancy; however, there is already a standard set of gestures that a user understands and expects when interacting with your applications. The GestureTransformEvent class can detect four different types of gestures: swipe, rotate, pinch and zoom, and pan. Swipe detects a single finger swiping across the screen in the left, right, up, or down direction. The rotate gesture allows you to place two fingers on an object and rotate one finger around the other to rotate the object. The pinch and zoom gesture enables you to zoom in and out of objects by moving your fingers closer or farther apart from each other. Finally, the pan gesture lets you pan an object in any direction with two fingers. Some of these gestures are supported in Windows 7, Mac OS X 10.5.3, and Windows Mobile 6.5, as well as on iPhone and Android devices. If you plan on supporting more than one platform, you should double check which gestures are fully supported on each before starting development.

NetConnection

Adobe has added NetConnection support for Android applications. The NetConnection class gives you the ability to connect to a Flash Media Server to create peer-to-peer applications, as well as view streaming video on your device. It will also allow you to communicate with Flash Remoting and the AMF protocol.

FLV

FLV is Adobe’s Flash Video format. There are two methods of playing an FLV video on a Android device. The first is to import the video onto the Timeline in an FLA file. This will create a MovieClip with your video in it, and you will be able to control it just as you would a normal MovieClip. The second method is to bundle the file with the application and load it at runtime. Both of these methods are covered in Chapter 8, “Working with Video,” later in the book.

Shared Objects

SharedObjects are Flash’s version of a browser cookie. They allow your Flash application to save user data on the device. This allows your application to load the save data when you come back to the application.

SQLite Database

The SQLite database is the most widely deployed SQL database in the world. When Adobe released AIR 1.0, it included the ability to communicate with SQLite databases from your applications. This gives you the ability to save large and complex data locally on the device. The Android platform also has SQLite libraries to develop with. A lot of the data on your device is stored in SQLite databases, such as your Contacts and Call History. Adobe has given us the ability to create, save, and load data from a SQLite database.

Check What APIs Are Not Available

Some of the AIR features that you are familiar with on the desktop are not currently available in the mobile profile of AIR. The omission of some of the features makes perfectly good sense, such as the concept of windows, as an Android application can have only one window, but some may not be so obvious. It is still the early days for the AIR mobile profile, so Adobe has decided to release the AIR Runtime without some of the features instead of having us wait longer for more features. Also, the AIR mobile profile started out with creating native iPhone applications. Apple placed certain restrictions on applications, which prevented Adobe from developing these features from the start. Adobe is committed to making the AIR Android Runtime a success and is constantly working to include new features and updates for developers to take advantage of.

HTML Loader

The HTMLLoader class provides AIR with the capability to render Web pages inside the application. Adobe has bundled a version of WebKit inside the AIR Runtime for the desktop, which provides a number of advanced features that are most likely not needed for mobile devices. In order to provide support for HTMLLoader, Adobe would have to bundle WebKit with the Android AIR Runtime, which would increase both its file size and the memory footprint. You can use the StageWebView class in order to load and display HTML text and Web pages within your application. However, the StageWebView class does not provide developers with the same level of communication between ActionScript and the JavaScript as HTMLLoader provides.

New AIR 2.0 Networking Classes

With the most recent release of AIR 2.0, Adobe has introduced some new networking classes. The ServerSocket class enables you to create your own socket server and have other AIR clients connect to it via the Socket class. This feature allows you to pass data to and from applications. The ServerSocket class is currently not available, but you can create a Socket in your Android application and connect to one running on your computer.

The DatagramSocket class enables you to send and receive UDP data. This can be used to create peer-to-peer applications or gaming. It is more unreliable than a TCP socket because you cannot guarantee the order of the data you will receive and lost packets are not retransmitted or even detected.

Adobe has also introduced a set of classes that can be used in conjunction with the new socket and server classes. For example, the NetworkInfo class provides you with a list of all available network interfaces available on the current machine. From those, you are able to find out what type of interface they are as well as the IP for the address. This comes in handy when you are creating peer-to-peer applications in which you may not know the IP address of the other peer.

There is technically no reason why these classes are not part of the Flash to Android offering, and they have been a highly requested feature. Adobe does plan on implementing these sometime in the future — when, however, is very hard to say.

Pixel Bender

Currently, Pixel Bender kernels and the Shader class are not supported in the Android AIR Runtime. These were originally not supported because the Apple App Store terms of use state that an application cannot execute any interpreted code. This may be something that Adobe visits with Android, but it is most likely lower on the priority list for Adobe and developers.

AS1 and AS2

This may come as a surprise to some developers, but there are still many developers who have not adopted AS3 and are still using AS2 or AS1. If you are one of these developers or have some older projects that you are looking to convert, you will need to start learning AS3 and start converting your projects. Currently, the AIR packager works only with AS3, and there will not be support for any earlier version of ActionScript. All the tasks and code in this book are in ActionScript 3, so if you are not familiar with it, I suggest you read up on ActionScript in order to get up to speed first; I recommend the book ActionScript: Your visual blueprint for creating interactive projects in Flash CS4 Professional, available from Wiley Publishing.

Alchemy

Adobe Alchemy is an Adobe Labs project, which enables developers to compile C and C++ code that would run in the ActionScript Virtual Machine (AVM). The exciting thing about this project is that it allows you to add functionality to the Flash Player that currently does not exist. Some community examples include being able to play external .wav audio files and open and preview .psd Photoshop files. Currently, this is not officially supported, but if there is some Alchemy library that you want to use, test it on your device to see if it does work.

Notifications

Android devices have a notifications area in the status bar, which can be used by applications to notify the user of an event. This is commonly used for new email notifications as well as application updates. Currently, only native Android applications can post notifications to the notification area, not AIR Android applications.

Widgets

You can develop many types of applications for the Android platform. As well as a regular application that a user would launch from the home screen or Android menu, the Android SDK allows you to develop widget applications and services. Widget applications are no more than a miniature application view that can be placed on the home screen. Widget applications provide users with small amounts of data or statuses, such as the weather or news. Currently, you cannot build widget applications with the Android AIR Runtime.

Become an Android Developer

If you want to submit your AIR Android applications to the Android Market, you must first create an Android developer account. The registration fee for the Android Market is $25.00 US and can be paid using Google Checkout during the registration process. This is a one-time fee and does not require a renewal every year like other mobile development platforms.

In order to sign up for an Android Market account, you must first log in to your existing Google account. If you do not have an account, you can click a link on the sign-up page to create one. After you are signed in with your Google account, you will be presented with the Getting Started page. This page has a form on it that will be used to create your Android Market account. The information will also determine how you appear to customers in the Market.

The developer name that you choose will appear underneath your application listing in the Android Market. Feel free to enter your name if you are an individual developer or your company name if you plan on selling your applications. Your developer name will also be a link that sends users to the URL provided in the Website URL field.

After you have filled out all the information correctly and submitted it, you will be brought through the payment process of your application. Google uses its Google Checkout service to process your payment. After your payment has been accepted, your application will be approved, and you will be brought to your developer home screen. From here, you can upload your applications to the Android Market. For more details on submitting your applications, see Chapter 17.

Become an Android Developer

Start the Registering Process

In a browser, go to http://market.android.com/publish.

Sign in with your Google account.

OR

Click Create an Account Now.

Note: If you already have a Google account, skip to step 10.

Create a Google Account

Type your current email address.

Type and confirm a password.

Click here and choose a location.

Type your birthday.

Scroll down and type the word shown in the image.

Click I Accept. Create My Account.

The Account Create Confirmation screen appears.

Click Click Here to Continue.

Create an Android Market Account

The Getting Started page appears.

Type a developer name.

Double check that your prefilled-in email address is correct.

Type the URL of your Web site.

Type your phone number.

Click Continue.

The Registration Fee page appears.

Click Continue.

The Google Checkout page appears.

Fill in all the necessary information and pay the $25.00 registration fee.

Your Android Market developer page appears.

• You can click Setup Merchant Account and follow the site’s instructions if you plan on selling your applications.

Extra

If you plan on selling your applications, you will need to set up a merchant account with Google Checkout. To set up your account, you will need to enter additional banking information as well as tax ID numbers. Make sure to have these ready before you start. If you plan on uploading only free applications, you can skip this step. To set up your merchant account, click the Setup Merchant Account link at the bottom of your developer home page. You will be presented with a form that you will need to completely fill out in order to create your account. The processing fee for the Android Market purchases is different from a regular Google Checkout purchase. When an application is sold in the Android Market, Google will keep 30% of the application price. For example, if your application is sold for $10.00, you will receive a payment of $7.00, and Google will keep $3.00 as a transaction fee. If you do not have all your business, banking, and tax information ready during the sign-up process, you can enter it any time to set up your merchant account. However, you will not be able to charge customers for your applications until you do.

Get the Android SDK

The Android SDK is not required to develop AIR Android applications; however, it is required to interact with your device in order to install your application. The Flash CS5 IDE uses the SDK to install the compiled Android application on your device when you publish. The SDK also includes an emulator that allows you to test your applications locally before testing on a device. It is highly recommend that you download the SDK before you begin developing your application.

The SDK can be downloaded from the Android Developers site. There is a separate download for Windows, Mac OS X, and Linux; simply choose the version for the operating system on which you will be developing. After the .zip file has finished downloading, extract it and place the extracted contents in a location where you can access it conveniently.

In the root of the SDK, there is a tools folder. This folder contains several of the tools or executables that you will use to interact with your device and the emulator. The android executable provides a user interface for managing the SDK. Running this application will bring up the Android SDK and AVD Manager window. From here, you can download all the up-to-date components of the SDK. Be sure to download all of the Android 2.2 API 8 packages. There is no need to download the documentation or the samples, but as long as you download the SDK platform, you should have everything you need in order to develop AIR Android applications.

Get the Android SDK

In a Web browser, go to http://developer.android.com/sdk/index.html.

Click the SDK package for your operating system.

The License Agreement page appears.

Click I Agree to the Terms of the Android SDK License Agreement.

Click Download.

The SDK is downloaded.

Extract the SDK .zip package.

On Mac OS X, in a Terminal window, run the android tool in the tools folder of the SDK.

OR

In Windows, execute the SDK Setup.exe at the root of the SDK folder.

The Android SDK and AVD Manager interface appears.

Click Available Packages.

Click the packages to download and install.

Click Install Selected.

The packages are downloaded and installed.

Extra

To make life easier when using the tools in the SDK through the command line, add the tools folder of the SDK to your System Path variable. This can be done on Mac OS X by creating a .bash_profile file in the root of your user directory and placing the following text in it, making sure to replace <PATH TO SDK> with the full path to the root folder of your SDK:

export PATH=${PATH}:<PATH TO SDK>/tools

This process for Windows is different for each version, and much more complicated. A Google search for “Adding folders to Path variable for Windows” will give you many results on how to do this.

Get the Android Eclipse Plug-in

The Android Eclipse Plug-in is primarily used for developing native Android applications with Java. However, it provides many valuable features that you can use to help debug your applications. This is especially useful if you are already using an Eclipse-based IDE, such as Flash Builder or FDT, as your primary ActionScript editor. Even if you are not currently using Eclipse, the Android plug-in can be useful if you are not comfortable with using the command line in order to interact with the SDK.

You can install the plug-in using the Install New Software option in Eclipse or your Eclipse-based IDE, such as FDT or Flash Builder. Have the URL https://dl-ssl.google.com/android/eclipse/ handy because you need to enter it in the Install dialog box. Eclipse searches this URL for any available Eclipse software and plug-ins. If the URL is

entered correctly, the Development Tools item will appear in the dialog box. You can select this to begin the download and installation process.

After the plug-in is downloaded and installed, you will be asked to restart Eclipse. When you restart, you will be prompted to enter the path to your Android SDK location. If you have not downloaded the Android SDK, see the preceding section, “Get the Android SDK,” for more details.

From Eclipse, you can access the Android SDK and AVD Manager window by selecting Window → Android SDK and AVD Manager from the main menu. In that window, you can set up new emulator instances.

The Eclipse plug-in also installs the DDMS (Dalvik Debug Monitor Service) perspective, which provides a number of different ways to interact with your device and emulator instances.

Get the Android Eclipse Plug-in

Get and Install the Plug-in

In Eclipse, click Help.

Click Install New Software.

The Install dialog box appears.

Type https://dl-ssl.google.com/android/eclipse/.

Click to check Developer Tools.

Click Next.

The Install Details page of the dialog box appears.

Click Finish.

The plug-in begins to download and install.

Set the Android SDK Path

Click Eclipse → Preferences from the main menu.

The Preferences dialog box appears.

Click Android.

Type the location of the directory of your Android SDK.

Click OK.

The path to the Android SDK is added.

Extra

The most useful feature of the DDMS perspective is the File Explorer panel. With a device selected, this panel will show the file system of that device. This can help you ensure that files are being created in the correct location. It also allows you to push and pull files to and from the device. By default, you will not have access to some of the folders on the device, such as the data directory. This is because that folder is owned by the root user of the device. In order to gain access to that folder, you must root your phone, which will void your warranty but also give you more access. Unless there is a very good reason for doing so, it is not recommended that you root your device. There are many tutorials online on how to do this if you are interested. Make sure to follow the instructions for your device, or it may result in a bricked device.

There are also many useful utilities in the DDMS perspective that can be used to debug your applications. For more details, see Chapter 16, “Debugging Your Application.”

Enable USB Debugging

In order to install applications on your device, you must first have it set up to enable debugging. Depending on which operating system you are currently developing on, the number of steps may vary.

If you are developing on a Windows computer, you will need to download and install the USB driver package using the Android SDK and AVD Manager. This process is the same as installing the additional Android SDK components. For more details, see the section “Get the Android SDK” earlier in this chapter. If you already completed the steps in that section and selected all the available packages, there is a good chance that you have already installed this. You can check to see if it has been downloaded by looking for the usb_driver directory in the root of your SDK. The process for installing the drivers is different for each version of Windows, and I suggest that you follow the instructions on the Android Developers site, http://developer.android.com/sdk/win-usb.html, in order to install them correctly.

If you are using a Mac OS X computer for development, you can skip having to install the USB drivers. The steps below should just work.

You can now connect your device to your computer with the USB cable included with your device. If it has connected correctly, you should see the USB symbol in the notifications area of the status bar. The next step is to turn on USB debugging when the device is connected. To do so, on your device, you go to the list of options that can be used during development and choose to debug applications on the device.

Enable USB Debugging

Connect your device with a USB cable.

Tap Settings.

The Settings application is launched.

Tap Applications.

The Application Settings are displayed.

Tap Development.

The Development settings are displayed.

Tap USB Debugging.

A confirmation dialog box appears.

Tap OK.

Your device is now ready for USB debugging.

Extra

There are two additional options in the Development list that are also very useful when developing and debugging applications. The Stay Awake option prevents the screen from going to sleep while it is connected to a USB cable or while charging. This can be useful when developing if you are testing and continuously reinstalling your application on the device. More times than not, the screen will go to sleep when you are compiling and installing your application. If you turn this on, make sure to turn it off when you are testing to see if your system idle code works correctly. For more details on setting the system Idle mode, see Chapter 12, “Using the Location and WiFi Features.”

The third option is Allow Mock Locations. If this option is checked, you can simulate the device being at a specific geolocation coordinate. This is extremely useful when testing geolocation features of your application without physically being at a specific location. For more details on simulating geolocation coordinates on your device, see the section “Debug with the Android Eclipse Plug-in” in Chapter 16.

Create an Android Virtual Device

Android Virtual Devices (AVDs) are configured emulator instances, which enable you to set up an emulator to mimic the behavior of an actual device. Creating an Android Virtual Device gives you full control over the entire specifications of the emulator. This allows you to set up an emulator just like a device that you may want to test your application on.

Each Android Virtual Device is made of up several parts. The first is which version of the SDK you want to use on the device. In order to make sure that the device has Android 2.2 or higher installed, choose a target with an API level greater or equal to 8. Next, you can specify a specific skin and screen resolution for your device. Several of the popular device screen resolutions come preinstalled with the SDK; however, you can enter a custom resolution if you prefer. You can also specify the size of the SD card that you want your device to have, and you can select an image of an SD Card in order to have it be populated with files when created. You can also adjust the hardware profile of the device. There are a number of items that you can add to your device, such as support for a directional pad, touch-screen capability, a GPS, a camera, a trackball, and keyboard support.

Google has done a great job in allowing you to model your Android Virtual Device after an actual device as closely as possible. This keeps you from having to have a physical device for all the different Android devices in order to test your application. However, this does not replace testing on an actual device, which should always be done.

Create an Android Virtual Device

In a Mac OS X Terminal window, type android and press Enter.

OR

In Windows, run the SDK Setup.exe executable in the root SDK directory.

The Android SDK and AVD Manager is launched.

Click Virtual Devices.

Click New.

The Create New Android Virtual Device (AVD) dialog box appears.

Type a name.

Click here and select a target, such as Google APIs (Google Inc.) – API Level 8.

Select a size for the SD Card, such as 9 MiB.

Click here and select a screen resolution, such as WVGA800.

Click New.

The hardware features dialog box appears.

Click here and select a hardware feature, such as Touch-Screen Support.

Click OK.

Repeat steps 8 to 10 for each hardware feature that you want to add.

You are returned to the Create New Android Virtual Device (AVD) dialog box.

Click Create AVD.

A result dialog box appears.

Click OK.

The AVD is created.

Extra

In order to see a complete list of all the available Android Virtual Devices, you can enter the following command in a Terminal or command prompt window:

android list avds

This will print out a list of the AVDs as well as their configurations. The name, path to the .avd file, the target framework and API, and the skin are displayed for each AVD.