50 Android Hacks
Carlos Sessa
Download from Wow! eBook
Copyright
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact
Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964.
2013 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Mannings policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.
| Manning Publications Co.20Baldwin RoadPO Box 261Shelter Island, NY 11964 | Development editor: Cynthia KaneTechnical proofreader: Cyril MottierCopyeditor: Benjamin BergProofreader: Katie TennantTypesetter: Dottie MarsicoCover designer: Marija Tudor |
ISBN 9781617290565
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 MAL 18 17 16 15 14 13
Dedication
Al milagro que hizo esto posible
(To the miracle that made this possible)
Brief Table of Contents
Table of Contents
Foreword
Android as an ecosystem is expanding rapidly in all directions. Every day manufacturers introduce new devices and form factors, consumers purchase and activate over one million devices, and users download and try new apps. Its the job of developers (yourself included, hopefully) to fill this ecosystem with beautiful, engaging, and deeply fulfilling applications through which users can better interpret and interact with their world.
As a platform, Android was birthed in late 2003 by former employees of Danger (the company behind the popular Sidekick phones). In 2005 the company driving Android was acquired by Google, and three years later the HTC Dream (G1) was released as the first consumer device running Android. Over the next three years the hardware and platform were heavily iterated, but Android remained solely a phone operating system.
In 2011 Google introduced two new form factors for the Android: tablets and TV. This represented the first official deviation from phones as the device of choice and sparked manufacturer interest in other devices. Android now runs on laptops, wrist-watches, video game consoles, and car stereos. It can only be expected that in the future the number of devices supporting Android will continue to grow.
As application developers, its extremely important that you understand the diversity of the platform and the direction in which its heading. Creating content on Android is no longer as simple as designing for a phone-sized screen held in portrait orientation. While this does mean more work for the developer creating apps, the end result is a vastly more pleasant experience for the user, regardless of which device your content is consumed on.
In developing applications there are three major things that youll need aside from your own creativity and desire to develop: the platform documentation, the open source community, and glue to hold everything together. The platform documentation is easy, since the latest version is always hosted at http://developer.android.com. The open source community is spread across GitHub, Google Code, Stack Overflow, and the like, providing libraries, code snippets, and design patterns for simplifying development. You still need something to tie these disjointed pieces together as one cohesive app. If it were as simple as arranging a few building blocks, everyone would be developing applications. This book is that glue.
Contained in the book are examples of how to solve common problems that arise in Android development. Some are relatively trivial and some quite complex. What they share, however, is being loosely or sparsely documented facets of app development which often cause developers pain. 50 Android Hacks is not meant as a sole resource for learning or mastering Android development, but rather exists to fill in the cracks.
Its a great task to craft an app thats dynamic enough to support Androids growing device diversity. With the knowledge provided by this book, accompanied by that of similar print and online sources, its my hope that youre more empowered to develop and publish apps. Beyond this, while I am a developer just like you, I am also an avid Android user and patiently await that next great application. Perhaps you will be the one to write it.
J AKE W HARTON
A NDROID E NGINEER
Preface
I started learning about Android back in 2009. Android version 1.5 had just been released, and it showed a lot of potential.
In July 2009, thanks to a friend living in Australia, I got my first Android-powered device, an HTC Magic with Android version 1.5. To be honest, it processed more slowly than I expected, but I started testing the APIs and creating apps that I wanted to have on my cell phone. I sensed that Android would get a lot of attention and I knew that if I managed to create an application, it would be available to a lot of people.
I was proved rightnot long afterward, there was a kick-off for Android development, which soon grew bigger and bigger. Suddenly a lot of tools and third-party libraries supporting the Android platform emergedeverything from game frameworks, like cocos2d-x, to build systems, like Apache Maven.
In November 2010 I was asked to review a book from Manning Publications called Android in Practice (www.manning.com/collins/). Delving deep into Mannings work, it occurred to me that I could write a book about Android development using a different approach. I wanted to imitate Joshua Blochs Effective Java (www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683), providing tips and patterns I had learned over all my years of developing for the Android platform.
Essentially, I wanted to gather together in one book every Android tip I have learned and provide some degree of documentation for it. Thats what 50 Android Hacks is all about: a collection of tips gathered in the process of developing different Android applications.
Something I enjoyed about Effective Java was that the book doesnt have any particular order and I could read various sections, learning something different from each of them. After some time, I would go back to the book and find a different application for the project I was working on. I kept that in mind while writing this book. I imagine the reader investigating a hack while going to work or before going to sleep, getting new ideas for the project theyre working on.
Im already using this book on my new projects, copying the sample code for certain tasks and using its examples to explain to my coworkers certain patterns. Its proven to be useful for myself, and I hope it will be useful for you as well.