Pro Android Python with SL4A." 第一章

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.

  本书是关于用Python和一点Javascript来开发Android。Java开发合情,当你只用来修改你的安卓设备的设置时,用这个有点小题大作了。 SL4A正是做这个特殊应用的。本书将你带领到SL4A,并且给你强大的自动化你的设备的能力,是你以前所认为不可能的。
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 entirelyopen 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.

为什么用? 不是所有人都是JAVA开发。它语言太庞大无没完全开源,需要编译。你可以需要一个其它的开发语言
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.

Google的SDK是针对Java开发人员,安卓市场上多数了是Java开发。第3章我会讲SDK并且自始至终会用到一些它 的工具。
SL4A当前支持以下语言: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 ofcode 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.

SL4A是针对所有想写脚本去完成自动化任务的人,它提供一个交互控制台。它使在安卓类的设备上用其它语言开发及交互的方法。
CHAPTER 1 ■INTRODUCTION
2
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.

安卓世界

2005年,Google买了安卓,并进入到移动系统开发领域。发展这么快是个奇迹。安卓社区在快速增长。
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 .apkfiles. These are really just .zipfiles containing everything
needed by the application. In fact, if you rename an .apkfile to .zip,you can open it with any archive
tool and examine the contents.

.APK文件 就是一个.zip文件,你可以查看它的内容。
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 rootingyour 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.

安卓设备默认是阻止修改系统文件。安卓OS是基础Linux,有很多功能你能在Linux桌面系统中找到类似的。你可以通过Root来获取全部功能。

SL4A不需要你使用一个Root过的设备。
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 ofabstraction 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.

安卓应用分析

安卓基于Linux,LInux已经提供了设备的驱动,内存、进程、网络、安全等管理。Linux是安卓系统的骨架,肌肉和器官。
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 a pplication ar chitecture is des igned to sim plify the r euse o f com ponents. Any
application  can publi sh it s ca pabilities, and any  other a pplication may th en make
use o f tho se  capabilities (subject to  security constraints enf orced  by the fr amework).
This same mechanism allows components to be replaced by the user.

下一包装层是DVM,它提供Java语言支持。它是所有进程最主要运行的地方,是大脑。所有安卓程序运行在一个Dvm的私有实例上。

安卓开发框架提供所有必须组件。安卓文档说:开发都通过同一个框架API.应用框架的设计是为了简化和重用组件。任何应用可以

发布功能,其它程序可以使用这些功能(当然要受到框架的安全约束)。同样的机制可以让用户替代某些组件。


Underlying all applications is a set ofservices and systems, including:
•  A rich and e xtensible s et 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, pr oviding access to non-code resources such as localized
strings, graphics, and layout files
CHAPTER 1 ■INTRODUCTION
3
•  A Notif ication Manager t hat enables all applications to  display custom alerts
in the status bar
•  An Activity Manager that manages  the lifecycle of applications and provides a
common navigation backstack”
1
All Android applications are based on three core components: activities, services, and receivers.
These core components are activated through messages called intents. SL4A gives you access to much of
the core Android functionality through its API facade, so it’s a good idea to understand some of the
basics. Chapters 3 and 5 look at the Android SDK and Android application programming interface (API)
in detail, so I’ll save the specifics for later. For now, I’ll introduce you to activities and intents, as they will
be used extensively.
Activities
The Android documentation defines an activityas “an application component that provides a screen
with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window
typically fills the screen but may be smaller thanthe screen and float on top of other windows.”
Android applications consist of one or more activities loosely coupled together. Each application
will typically have a “main” activity that can, in turn, launch other activities to accomplish different
functions.
Intents
From the Google documentation: “An intent is a simple message object that represents an intentionto
do something. For example, if your application wants to display a web page, it expresses its intentto
view the URI by creating an intent instance and handing it off to the system. The system locates some
other piece of code (in this case, the browser) that knows how to handle that intent and runs it. Intents
can also be used to broadcast interesting events (such as a notification) system-wide.”
An intent can be used with startActivityto launch an activity, broadcastIntentto send it to any
interested BroadcastReceivercomponents, and startService(Intent)or bindService(Intent,
ServiceConnection, int)to communicate with a backgroundservice. Intents use primary and
secondary attributes that you must provide in the form of arguments.
There are two primary attributes:
•  action:The general action to be performed, such as VIEW_ACTION, EDIT_ACTION,
MAIN_ACTION, and so on
•  data:The data to operate on, such as a person record in the contacts database,
expressed as a Uniform Resource Identifier (URI)
1
http://developer.android.com/guide/basics/what-is-android.html
CHAPTER 1 ■INTRODUCTION
4
There are four types of secondary attributes:
•  category:Gives additional information about the action to execute. For example,
LAUNCHER_CATEGORYmeans it should appear in the Launcher as a top-level
application, while ALTERNATIVE_CATEGORYmeans it should be included in a list of
alternative actions the user can perform on a piece of data.
•  type:Specifies an explicit type (a MIME type) of the intent data. Normally, the
type is inferred from the data itself. By setting this attribute, you disable that
evaluation and force an explicit type.
•  component:Specifies an explicit name of a component class to use for the intent.
Normally this is determined by looking at the other information in the intent (the
action, data/type, and categories) and matching that with a component that can
handle it. If this attribute is set, none of the evaluation is performed, and this
component is used exactly as is. By specifying this attribute, all the other intent
attributes become optional.
•  extras:A bundle of any additional information. This can be used to provide
extended information to the component. For example, if we have an action to
send an e-mail message, we could also include extra pieces of data here to supply
a subject, body, and so on.
SL4A History
SL4A was first announced on the Google Open Sourceblog in June of 2009 and was originally named
Android Scripting Environment (ASE).It was primarily through the efforts of Damon Kohler that this
project came to see the light of day. Others have contributed along the way as the project has continued
to mature. The most recent release as of this writing isr4, although you’ll also find experimental versions
available on the SL4A web site (http://code.google.com/p/android-scripting).
SL4A Architecture
At its lowest level, SL4A is essentially a scriptinghost, which means that as an application it hosts
different interpreters each of which processes a specific language. If you were tobrowse the SL4A source
code repository, you would see a copy of the source tree of each language. This gets cross-compiled for
the ARM architecture using the Android Native Development Kit (NDK) and loads as a library when SL4A
launches a specific interpreter. At that point,the script will be interpreted line by line.
The basic architecture of SL4A is similar to what you would see in a distributed computing
environment. Figure 1-1 shows in pictorial form the flow of execution when you launch SL4A and then
run a script (in this case, hello.py). Every SL4A script must import or source an external file, such as
android.pyfor Python, which will define a number of proxy functions needed to communicate with the
Android API.
The actual communication between SL4A and the underlying Android operating system uses a
remote procedure call (RPC) mechanism and JavaScript Object Notation (JSON). You normally find RPC
used in a distributed architecture in which information is passed between a client and a server. In the
case of SL4A, the server is the Android OS, and the clientis an SL4A script. This adds a layer of separation
between SL4A and the Android OS to prevent any malicious script from doing anything harmful.
CHAPTER 1 ■INTRODUCTION
5
Security is a concern and is one of the reasons that SL4A uses the RPC mechanism. Here’s how the
SL4A wiki describes it:
“RPC Authentication: SL4A enforces per-script  security sandboxing by requiring all
scripts t o be authenti cated by the  corresponding RPC server. In ord er for the
authentication to succ eed, a script has to send the corr ect hand shake se cret to th e
corresponding server. This is accomplished by:
1.  reading the AP_HANDSHAKEenvironment variable.
2.  calling th e  RPC me thod _authenticatewith the  value  of AP_HANDSHAKEas an
argument.
The _authenticatemethod must be the fi rst RPC call and should take place during the
initialization of the Andr oid library. F or example, see Rhin o’s o r Python’s A ndroid
module”.
2
Figure 1-1.SL4A execution flow diagra

转载于:https://www.cnblogs.com/shenjunjian/p/3509035.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值