with SL4A - Springer978-1-4302-3570-5/1.pdf · ~CONTENTS UiFacade 132 WakeLockFacade 137...
Transcript of with SL4A - Springer978-1-4302-3570-5/1.pdf · ~CONTENTS UiFacade 132 WakeLockFacade 137...
Pro Android Pythonwith SL4A
Paul Ferrill
Apress·
Pro Android Python with SUA
Copyright © 2011 by Paul Ferrill
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,electronic or mechanical, including photocopying, recording, or by any information storage or retrievalsystem, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-4302-3569-9
ISBN-13 (electronic): 978-1-4302-3570-5
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbolwith every occurrence of a trademarked name, logo, or image we use the names, logos, and images onlyin an editorial fashion and to the benefit of the trademark owner, with no intention of infringement ofthe trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they arenot identified as such, is not to be taken as an expression of opinion as to whether or not they are subjectto proprietary rights.
President and Publisher: Paul ManningLead Editor: Tom WelshTechnical Reviewer: Justin GrarnmensEditorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick,
Jonathan Hassell, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, JeffreyPepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, MattWade, Tom Welsh
Coordinating Editors: Mary Tobin, Corbin CollinsCopy Editor: Nancy SixsmithProduction Support: Patrick CunninghamIndexer: SPI GlobalArtist: April MilneCover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street,6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springersbm. com, or visit www.springeronline.com.
For information on translations, please e-mail [email protected], or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.eBook versions and licenses are also available for most titles. For more information, reference ourSpecial Bulk Sales-eBook Licensing web page at www.apress.com/bulk-sales.
The information in this book is distributed on an "as is" basis, without warranty. Although everyprecaution has been taken in the preparation of this work, neither the author(s) nor Apress shall haveany liability to any person or entity with respect to any loss or damage caused or alleged to be causeddirectly or indirectly by the information contained in this work.
The source code for this book is available to readers at www.apress.com. You will need to answer questions
pertaining to this book in order to successfully download the code.
To my wife, Sandy, for your tireless support ofme and ourfamily. I could not have done this without you.And to my wondeiful children who put up with a preoccupied daddy for way too long.
-Paul Ferrill
iv
Contents at aGlance
About the Author ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• xi
About the Technical Reviewer xii
Acknowledgments ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• xiii
Preface •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• xiv
-Chapter 1: Introduction •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••1
.f Chapter 2: Getting Started 27
'~Chapter 3: Navigating the Android SDK 57
i Chapter 4: Developing with Eclipse 83
Chapter 5: Exploring the Android APi •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 113
_Chapter 6: Background Scripting with Python 139
--;:,~ Chapter 7: Python Scripting Utilities ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 165
-<~ Chapter 8: Python Dialog Box-based GUls 195
Chapter 9: Python GUls with HTML•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 221
-'8 Chapter 10: Packaging and Distributing 249
Index•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••273
Contents
About the Author ..............................•....................••............................•.•.•.••......••••••.•xi
About the Technical Reviewer xii
Acknowledgments ............................•......................................................................xiii
Preface .....................................•..............................................................................xiv
Chapter 1: Introduction 1
Why SL4A? 1
The World of Android 2
Android Application Anatomy 2
Activities 3
Intents 3
SL4A History 4
SL4A Architecture 4
SL4A Concepts 6
JavaScript Object Notation (JSON) 6
Events 7
Languages 7
Beanshell 2.0b4 7
Lua 5.1.4 8
Perl 5.1 0.1 9
PHP 5.3.3 11
Rhino 1.7R2 11
v
"CONTENTS
JRuby 1.4 12
Shell 13
Python 13
Summary 26
",:Chapter 2: Getting Started 27
Installing SL4A on the Device 27
Installing the Android SDK 39
Linux 39
Mac as x 41
Windows 41
Installing Python 43
Remotely Connecting to the Device 45
Device Settings 49
Executing Simple Programs 51
Summary 55
;·}'Chapter 3: NaVigating the Android SDK 57
Wading Through the SDK Documentation 57
Examining the Different SDK Components 59
Testing With the Android Emulator 60
Android Debug Bridge...............................•.............................................................................................68
Dalvik Debug Monitor Service (DDMS) 79
Summary 81
~Chapter 4: Developing with Eclipse 83
Installing Eclipse on a Development Machine 83
Eclipse Basics 88
Perspectives 90
Projects 93
vi
sCONTENTS
Android Development Toolkit. 94
Using Pydev 99
Using MUltiple File Types in Eclipse 107
Summary 110
f Chapter 5: Exploring the Android APi 113
Exploring the Android APls 115
Android Facades 116
ActivityResultFacade 118
AndroidFacade 118
ApplicationManagerFacade 121
BatteryManagerFacade 121
BluetoothFacade 123
CameraFacade 123
CommonlntentsFacade 124
ContactsFacade 125
EventFacade '" 127
EyesFreeFacade 127
LocationFacade 127
MediaPlayerFacade 128
MediaRecorderFacade 128
PhoneFacade 128
PreferencesFacade 128
SensorManagerFacade 129
SettingsFacade 130
SignalStrengthFacade 130
SmsFacade 131
SpeechRecognitionFacade 132
TextToSpeechFacade 132
ToneGeneratorFacade 132
vii
~CONTENTS
UiFacade 132
WakeLockFacade 137
WebCamFacade 137
WifiFacade 137
Summary 138
-Chapter 6: Background Scripting with Python 139
Background Tasks 139
Triggers 141
Orientation-based Actions 142
Location-based Actions 145
Time-based Actions 146
Elapsed Time-based Triggers 148
FTP File Sync Tool. 150
Syncing Photos with Flickr 158
Syncing with Google Docs 160
AStartup Launcher 162
Summary 164
vqChapter 7: Python Scripting Utilities 165
Python Libraries 165
E-mail-Based Applications 168
Location-Aware Applications 172
Tweet My Location 172
Killing a Running App 186
URL File Retriever 188
Python FTP Server 190
Summary 194
viii
",-CONTENTS
Chapter 8: Python Dialog Box-based GUls 195
UI Basics 195
Book Title Search 199
Convenience Dialog Boxes 201
Progress Dialog Boxes 203
Modal versus Non-Modal Dialog Boxes 205
Options Menu 207
File Listing with dialogCreateAlert 207
Dialog Boxes as Python Objects 209
Podplayer App 213
Building the mysettings App 216
Summary 220
-'ill Chapter 9: Python GUls with HTML 221
HTML and Basic Information Display 221
HTML and JavaScript. 224
HTML GUI Form Basics 226
Simple HTML Forms 228
Cascading Style Sheets 230
SMS Merger 233
Summary 247
ill Chapter 10: Packaging and Distributing 249
QR Codes 249
Application Packages 251
Packaging Your Own Application 264
Building with Ant 264
Compiling SL4A 266
Finishing Touches 269
ix
x
[CONTENTS
Winding Down 271
Summary 271
Index................................................•.........•............................................................273
About the Author
:Paul Ferrill has a BS and MS in electrical engineering and has beenwriting about computers for more than 25 years. He currently serves asCTO for Avionics Test and Analysis Corporation, working on multiple DoDprojects. Software development has been his primary focus, along witharchitecting large-scale data management and storage systems. He alsoserves on several DoD standards committees, providing input to the nextgeneration of data recording and transmission standards.
He has a long history with both Microsoft and open sourcetechnologies. His two favorite languages are Visual Basic and Python. He'shad articles published in PC Magazine, PC Computing, InfoWorld,Computer World, Network World, Network Computing, Federal ComputerWeek, Information Week, and multiple web sites.
xi
xii
About the Technical Reviewer
Justin Grammens has been writing software for 12 years, holds a mastersdegree in Software Systems, and has a patent pending on the process of asystem to collect and rate digital media. He has written applications for avariety of mobile platforms in a number of different market sectors and is thecofounder of Recursive Awesome, LLC; owner of Localtone, LLC; and founderof Mobile Twin Cities.
Justin has built online e-commerce systems, real-time mappingsolutions, large-scale tax accounting software, and technology for Internetradio stations. Having worked with Android since version l.0, Justin hasspoken on mobile technology at conferences and software developmentgroups since 2008.
Justin has developed Android applications for Best Buy, McDonald's,BuzzFeed, and Consolidated Knowledge; and is co-creator of a cross-platform
streaming video service called Mobile Vidhub. Justin is employed by Code 42 as a Director of MobileTechnology and lives in S1. Paul, MN, with his wife.
Acknowledgments
I would like to acknowledge the excellent staff at Apress who managed to get this book completed ontime through multiple delays and reworking of the original title. You've made the process much lessfrightening for a first-time author than I expected.
A special thanks goes to coordinating editors Mary Tobin and Corbin Collins, and to Tom Welsh, thelead editor.
I'd also like to thank Frank Pohlmann for convincing me to do this project in the first place.Thank you to the technical reviewer, Justin Grammens, for a keen set of eyes and helpful comments.
A big thank you to Robbie Matthews, who has become one of the primary contributors to the SUAproject and provided help when things didn't make sense.
Thanks also to the folks at TechSmith, and Betsy Weber in particular, for their fantastic Snagitproduct, without which the screenshots would have been so much harder.
Final thanks go to my son Micah Ferrill for his help with the Python code.
xiii
xiv
Preface
It's no secret that traditional computing patterns are undergoing a radical change. The proliferation ofsmartphones with ever-increasing processing power will only accelerate the process. Tablet devices haveseen a much broader adoption as extensions of the smartphone platform where previous attempts todownsize general-purpose computers failed. While the operating system of the most popular mobiledevices may be different from the user's perspective, it has more in common with a desktop system thanyou might think.
Google's Android platform has seen a huge increase over the last year and is challenging Apple's iOSfor market share. Apple's wide lead in applications has been steadily dwindling although the jury is stillout when it comes to quality. Building those applications has, for the most part, been restricted toObjective C for iOS and Java for Android. There are a few other options ifyou take into consideration theMonoTouch and MonoDroid projects, but that's about it.
Mobile devices will probably never completely replace traditional computers, although the divisionof activity will continue to swing toward the one you have access to the most. This book is aboutbringing some of the flexibility you get with a desktop computer in the form of writing simple programsor scripts to accomplish a specific task. I know I've learned a lot along the way, and it is my sincere hopethat through reading this book you will glean a thing or two as well.