Apress Pro Android Python with SL4A (2011).pdf

(5132 KB) Pobierz
Build Android Apps
with Python
Pro
Pro
Android Python
with SL4A
Paul Ferrill
780207305.037.png 780207305.045.png 780207305.046.png 780207305.047.png 780207305.001.png
 
 
 
 
 
 
 
780207305.002.png 780207305.003.png 780207305.004.png 780207305.005.png 780207305.006.png 780207305.007.png 780207305.008.png 780207305.009.png 780207305.010.png 780207305.011.png 780207305.012.png
 
780207305.013.png 780207305.014.png
 
 
 
 
 
 
780207305.015.png 780207305.016.png 780207305.017.png 780207305.018.png 780207305.019.png 780207305.020.png 780207305.021.png 780207305.022.png 780207305.023.png 780207305.024.png 780207305.025.png 780207305.026.png 780207305.027.png 780207305.028.png 780207305.029.png 780207305.030.png 780207305.031.png
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
780207305.032.png 780207305.033.png 780207305.034.png 780207305.035.png 780207305.036.png 780207305.038.png 780207305.039.png 780207305.040.png 780207305.041.png
Contents at a Glance
About the Author ....................................................................................................... xi
About the Technical Reviewer .................................................................................. xii
Acknowledgments ................................................................................................... xiii
Preface .................................................................................................................... xiv
Chapter 1: Introduction ........................................................................................... 1
Chapter 2: Getting Started ..................................................................................... 27
Chapter 3: Navigating the Android SDK ................................................................. 57
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 GUIs .......................................................... 195
Chapter 9: Python GUIs with HTML ...................................................................... 221
Chapter 10: Packaging and Distributing .............................................................. 249
Index ....................................................................................................................... 273
iv
780207305.042.png
C H A P T E R 1
Introduction
This book is about writing real-world applications for the Android platform primarily using the Python
language and a little bit of JavaScript. While there is nothing wrong with Java, it really is overkill when all
you need to do is turn on or off a handful of settings on your Android device. The Scripting Layer for
Android (SL4A) project was started to meet that specific need. This book will introduce you to SL4A and
give you the power to automate your Android device in ways you never thought possible.
Why SL4A?
One of the first questions you probably have about this book is, “Why would I want to use SL4A instead
of Java?” There are several answers to that question. One is that not everyone is a fan of Java. The Java
language is too heavyweight for some and is not entirely open source. It also requires the use of an edit /
compile / run design loop that can be tedious for simple applications. An equally legitimate answer is
simply “I want to use X”, where X could be any number of popular languages.
Google provides a comprehensive software development kit (SDK) aimed specifically at Java
developers, and most applications available from the Android market are probably written in Java. I’ll
address the Android SDK in Chapter 3 and use a number of the tools that come with it throughout the
book.
Note SL4A currently supports Beanshell, JRuby, Lua, Perl, PHP, Python, and Rhino.
SL4A is really targeted at anyone looking for a way to write simple scripts to automate tasks on an
Android device using any of the supported languages, including Java through Beanshell. It provides an
interactive console in which you can type in a line of code and immediately see the result. It even makes
it possible, in many cases, to reuse code you’ve written for a desktop environment. The bottom line is
that SL4A makes it possible both to write code for Android-based devices in languages other than Java
and to do it in a more interactive way.
1
780207305.043.png 780207305.044.png
 
CHAPTER 1
INTRODUCTION
The World of Android
Google jumped into the world of mobile operating systems in a big way when it bought Android, Inc. in
2005. It’s really pretty amazing how far it has come in such a short time. The Android community is huge
and has spawned a wide range of conferences, books, and support materials that are easily available
over the Internet.
This is a good point to define a few terms that you’ll see throughout the rest of this book. Android
applications are typically packaged into .apk files. These are really just .zip files containing everything
needed by the application. In fact, if you rename an .apk file to .zip, you can open it with any archive
tool and examine the contents.
Most Android devices come from the manufacturer with the systems files protected to prevent any
inadvertent or malicious manipulation. The Android operating system (OS) is essentially Linux at the
core and provides much of the same functionality you would find on any Linux desktop. There are ways
to unlock the system areas and provide root , or unrestricted, access to the entire filesystem on an
Android device. This process is appropriately called rooting your device, and once complete, the device
is described as rooted . SL4A does not require a rooted device, but will work on one if you have chosen
this path.
Android Application Anatomy
Android is based on the Linux operating system (at the time of writing, version 2.6 of the Linux kernel).
Linux provides all the core plumbing such as device drivers, memory and process management, network
stack, and security. The kernel also adds a layer of abstraction between the hardware and applications.
To use an anatomical analogy, you might think of Linux as the skeleton, muscles, and organs of the
Android body.
The next layer up the Android stack is the Dalvik Virtual Machine (DVM). This piece provides the
core Java language support and most of the functionality of the Java programming language. The DVM is
the brains in which the majority of all processing takes place. Every Android application runs in its own
process space in a private instance of the DVM. The application framework provides all the necessary
components needed by an Android application. From the Google Android documentation:
“Developers have full access to the same framework APIs used by the core applications.
The application architecture is designed to simplify the r euse of components. Any
application can publish its capabilities, and any other application may th en make
use of those capabilities (subject to security constraints enforced by the framework).
This same mechanism allows components to be replaced by the user.
Underlying all applications is a set of services and systems, including:
A rich and e xtensible set of Views th at can be used to build an ap plication,
including lis ts, grids, tex t boxes, butt ons, and ev en an emb eddable w eb
browser
Content Pr oviders that enable a pplications to a ccess dat a f rom other
applications (such as Contacts) or to share their own data
A Resource Manager, providing access to non-code resources such as localized
strings, graphics, and layout files
2
Zgłoś jeśli naruszono regulamin