现在的位置: 首页 > 综合 > 正文

Android的一本很好的英文原著Andbook 我手动将其翻译了 方便以后查阅

2012年05月16日 ⁄ 综合 ⁄ 共 23627字 ⁄ 字号 评论关闭

andbook!

release.002

 

title_skateboard.png

Android Programming

with Tutorials from the
anddev.org-Community.

written by Nicolas Gramlich

 

Check for the latest version on

http://andbook.anddev.org

 


 

Content

Foreword / How to read this doc .................................................
5

Introduction................................................................................
6

What is Android – a GPhone?
...................................................... 7

Android from above ........................................................................
8

Openness
.......................................................................................................
8

All applications are created equal
................................................................. 9

Breaking down application boundaries
......................................................... 9

Fast & easy application development
........................................................... 9

The first weeks ..............................................................................
12

Dalvik.equals(Java) == false
........................................................... 13

Differences to a normal JavaVM
................................................................. 13

The Android Code Challenge
..................................................... 14

Creating Android Applications
.................................................. 15

Anatomy of an Android Application
............................................... 15

Activity
.........................................................................................................
15

Intent Receiver
............................................................................................
17

Service .........................................................................................................
17

Content Provider
.........................................................................................
18

Android User Interfaces .................................................................
19

Hierarchy of Screen Elements
..................................................................... 19

Comparing Android UI Elements to Swing UI
Elements .............................. 22

The AndroidManifest.xml
......................................................... 23

General
.........................................................................................
24

<manifest>
...................................................................................................
25

<uses-permission>
.......................................................................................
25

<permission>
...............................................................................................
25

<instrumentation>
.......................................................................................
25

<application>
...............................................................................................
25

<meta-data> ................................................................................................
27

<receiver>
....................................................................................................
27

<service>
......................................................................................................
27

<provider>
...................................................................................................
28

Resources and the magic R.java
................................................ 29

Resources
.....................................................................................
29

List of resources
...........................................................................................
29

Using resources in code ...............................................................................
30

Referencing Resources
................................................................................
31

Alternate Resources & Localization
................................................ 32

The magic R.java
........................................................................... 33

Hello World – The Android way.
................................................ 34

Installing the Android SDK
............................................................. 35

The Android Development Tools (ADT)
....................................................... 35

Installation done, let’s do some code
............................................. 37

Creating a new Android Project
.................................................................. 37

Running your first application
..................................................................... 43

UIs the Java way
........................................................................... 45

System.out.println(…) ?
................................................................. 46

The LogCat
...................................................................................................
46

Using Intents
............................................................................ 49

Starting (Sub)Activities
.................................................................. 49

Finding XML-defined Views in Java-Code
.................................................... 52

Handling Clicks to Views
..............................................................................
53

Returning values from SubActivities
........................................................... 56

Passing Data to (Sub)Activities
...................................................... 58

Important Layouts and ViewGroups
.......................................... 60

The RelativeLayout
........................................................................ 60

Bibliography
.............................................................................
61

Lost chapters
............................................................................ 62

The communities
.......................................................................... 62

wink.gif

nuts.gif

explicit_code.png

Foreword / How to read this doc

前言/如何阅读本文档

This document was written for developers
who have worked with Java.

before and want to start developing for the
Android Platform. I tried to

make this as much “hands on” as possible,
placing example codes

everywhere it fit. Also I tried to insert
as many picture as possible,

because they liven up the learning process
and relax the reader’s eyes.

这个文档是为那些做过java开发并且向继续在Android平台开发的开发者而写的。我把示例代码放在每一个适合的地方,便于让开发更加容易上手。此外,我也试着插入尽可能多的图片,因为图片能够使学习过程更加生动并且让读者能轻松的阅读。

But unfortunately coding is not everything;
one has to learn about

some basic facts of the Android Platform to
fully understand. That is

what is described on the first ~XXX pages.
It is not necessary to read all

those describing pages, but it is
preferable. You could decide to treat it as

a kind of reference. What you would read
there is also explained when it

occurs during the “hands on”-process. So
you could directly start at Hello

World – The Android Way.

但不幸的编码不是万能的,人们必须了解在Android平台的一些基本事实,以便于充分理解。这就是在最开始的部分要描述的内容。虽然不必要全部阅读这些描述部分,但是最好还是读一下。你可以决定将其视为一种参考,你在这些部分读到的内容也会在之后实际动手的过程中有相关解释。因此,你可以直接开始你的Android旅程的第一个程序Hello World

All codes you see in this document (the
whole workspace) will be

available on:

所有代码你在这个文档中看到的(整个工作区)将会在下面的网站中提供。

http://andbook.anddev.org/sources/

 

Have fun reading…

接下来,请尽情地在阅读中寻找你的快乐吧

 

Introduction

简介

The Open Handset Alliance (1) released the
Google Android SDK on

November 12th, 2007, having announced it
about a week before. The

impact was unbelievable, almost every
IT/programming-related news-

page dumped a news-post about the SDK
release – the Google Groups (2)

was overwhelmed with over 2000 Messages
within the first two Days.

The idea of the Android Platform was and
still is amazing and is of course

attracting more and more programmers every
day. Especially the open

architecture based on Intents and the
possibility to replace even the

Home-application grant a really large
amount of flexibility to the whole

platform.

一个星期前的报道宣称,开放手机联盟在20071112日发布了Google Android SDK。其影响真是令人难以置信,几乎所有IT和编程有关的页面都开辟专栏报道有关的SDK发布信息,而Google的工作小组更是在前两天被超过2000条的邮件弄的不堪重负。Android平台的想法过去并且现在仍然是令人惊异的并且每天都吸引着越来越多的程序员的加入。尤其是基于意图和甚至可能替代桌面应用的开发架构保证了真正的整个平台的灵活性。

Android – Imagination
is the limit”1

Android-想象的极限。

 

1 Nicolas Gramlich – anddev.org Site-Admin

尼古拉格拉姆利克 - anddev.org网站管理员

What is Android – a GPhone?

什么是Android 是一个google的手机GPhone?

The weeks and months before Google released
the Android SDK there

had been a lot of rumors about a so called GPhone.
It was said to be a

mobile device manufactured by Google
providing free communication by

showing context-sensitive advertisements to
the user on the device

itself.

几个星期和几个月前谷歌发布了Android SDK一直有很多传言这只是所谓的Gphone。据说这是一个由google生产的移动设备,它通过在设备上显示上下文敏感的广告给用户提供免费的通信。

Picture 1 Render of a potential GPhone

 

But on November 5th 2007 Andy Rubin2
announced:

但在2007115日安迪Rubin2宣布:

[The] Android
[Platform] – is more significant and ambitious than a single

phone.”

该机器人平台 比单一手机更有意义并且更有雄心。

Google within the Open Handset Alliance
(OHA) delivers a complete set

of software for mobile devices: an
operating system, middleware and key

mobile applications. What was released a
week later was not a final

product, but a “First Look SDK” what many
did not realize. Major news

sites grabbed the discomforts of some
developers who said that Android

is full of bugs and heavily lacks of
documentation. But the majority says

that Android is not buggier than any other
software at this stage.

谷歌开放手机联盟提供了一套完整用于移动设备的软件:操作系统,中间件和关键
移动应用。发布一周之后仍然不是一个最终产品,但是,在“第一印象的SDK”里,很多人都没有意识到。主要的新闻网站抓住一些开发人员对 Android 充满了错误并且严重缺乏的文档的不适。但多数不说,Android是在现阶段比任何其他软件bug更少。

 

2 Andy Rubin – Google Director of Mobile
Platforms

安迪鲁宾 - 谷歌移动平台总监

Android from above

Let’s take a look at what the OHA
emphasizes on its Android Platform:

从上面的android介绍,让我们再看一看,开放手机联盟强调的Android平台的外观。

Openness

开放性

Android was built from
the ground-up to enable developers to create

compelling mobile applications that take
full advantage of all a

handset has to offer. It is built to be
truly open. For example, an

application could call upon any of the
phone's core functionality such

as making calls, sending text messages, or
using the camera, allowing

developers to create richer and more cohesive
experiences for users.”

This is true, as a developer you can do
everything, from sending short

messages with just 2 lines of code, up to
replacing even the HOME-

Screen of your device. One could easily
create a fully customized

operating system within weeks, providing no
more of Google’s default

application to the user.

Android从建立就是为了让开发人员能够充分利用移动设备可以提供的功能实现一些应用。它将是一个真正开放的平台。例如,应用程序可以调用手机的核心功能,如任何打电话,发送短信,或使用相机时,允许开发者创建更丰富,更具亲和力的用户体验应用。这是真的,作为一个开发者你可以做一切事情。发送一行短消息仅仅只需要2行代码,甚至正取代设备桌面屏幕应用。人们可以在几周内很容易地创建一个完全个性化定制操作系统,没有谷歌提供的默认更多应用给用户。

Android is built on the
open Linux Kernel. Furthermore, it utilizes a

custom virtual machine that has been
designed to optimize memory

and hardware resources in a mobile
environment. Android will be

open source; it can be liberally extended
to incorporate new cutting

edge technologies as they emerge. The
platform will continue to

evolve as the developer community works
together to build innovative

mobile applications.”

Android是建立在开放的Linux内核。此外,它采用一自定义虚拟机的设计以优化移动环境的内存和硬件资源。Android将会是开源的,它可以随着先进技术的出现进行自由扩展。该平台将会继续演变为一个一起工作的开发者社区,来创立更多新的移动应用。

Here Google is talking of the so called
Dalvik virtual machine (DalvikVM),

which is a register based virtual machine,
designed and written by Dan

Bornstein and some other Google engineers,
to be an important part of

the Android platform. In the words
“register based” we find the first

difference to normal Java virtual machines
(JVM) which are stack based.

See the “Dalvik.equals(Java) ==
false”-chapter for more details on that

issue.

谷歌提出的是这里的所谓的Dalvik虚拟机(DalvikVM)是基于寄存器的虚拟机,它由丹伯恩斯坦和其他一些谷歌工程师设计并编码,是Android平台一个重要组成部分。在该平台基于寄存器的设计中,我们发现了它区别于java虚拟机的第一个不同点那就是java虚拟机是基于堆栈设计的。在文章的“Dalvik.equals(Java)
== false”
这章可以看到更加详细的信息。

All applications are created equal

所有应用都是平等的。

Android does not differentiate
between the phone's core applications

and third-party applications. They can all
be built to have equal access

to a phone's capabilities providing users
with a broad spectrum of

applications and services. With devices
built on the Android Platform,

users will be able to fully tailor the
phone to their interests. They can

swap out the phone's home screen, the style
of the dialer, or any of

the applications. They can even instruct
their phones to use their

favorite photo viewing application to
handle the viewing of all

photos.”

Android不会区分手机的核心应用和第三方应用程序。他们都可以有平等机会使用手机的功能来提供广泛的用户应用和服务。设备建立在Android的平台,用户将能够根据自己的喜好自由定制手机。他们可以掉了手机的主屏幕,该拨号器,或任何风格申请。他们甚至可以指导他们使用他们的手机
最喜欢的照片浏览应用程序来处理所有观看照片。”

Once again this is all true. Developers can
100% customize their Android-

Device. The Android System Communication is
based on so called Intents,

which are more or less just a String (with
some data attached) which

defines an action that needs to be handled.
An example for this is:

android.provider.Telephony.SMS_RECEIVED”

再一次说道,这是真正的,开发人员可以100%定制自己Android装置。Android的系统通信时基于所谓的意图,意图或多或少只是一个字符串(有一些附加的数据),其中定义了一个动作,需要加以处理。一个很好的例子来说明就是:

android.provider.Telephony.SMS_RECEIVED

 

One can simply listen on that Intent by
writing about 5 lines of

definitions. The system would then recognize
that there is more than one

application that wants to handle that
Intent and ask the user to choose

which one he or she would like to handle
the Intent.

程序员可以通过5行定义就能监听这个意图。系统便会知道有超过一个应用程序需要处理的意图,并且要求用户选择一个应用程序来监听这个意图。

Breaking down application boundaries

打破应用程序之间的边界

Android breaks down the
barriers to building new and innovative

applications. For example, a developer can
combine information from

the web with data on an individual's mobile
phone - such as the user's

contacts, calendar, or geographic location
- to provide a more relevant

user experience. With Android, a developer
could build an application

that enables users to view the location of
their friends and be alerted

when they are in the vicinity giving them a
chance to connect.”

 

Android打破了创立新的和创新应用的障碍。例如,一个开发人员可以把从网页上获取的信息与个人用户的移动电话,比如联系人信息,日历或者地理位置信息结合起来,带来更为全,面的用户体验。使用Android,一个开发者可以建立一个应用程序,让用户查看他们的朋友的位置,并且当他们在附近时进行提醒让他们有机会进行连接。

Fast & easy application development

快捷,方便的应用开发

Android provides access
to a wide range of useful libraries and tools

that can be used to build rich
applications. For example, Android

enables developers to obtain the location
of the device, and allows

devices to communicate with one another
enabling rich peer-to-peer

social applications. In addition, Android
includes a full set of tools that

have been built from the ground up
alongside the platform providing

developers with high productivity and deep
insight into their

applications.”

Android 提供了丰富的有用的库和工具来建立丰富多彩的应用。

比如说,Android允许开发者能够获得设备的位置,并允许设备互相沟通以实现点对点的社会应用。此外,Android包括了一个完整的从底层构建的平台的工具集合,为开发者在他们的应用程序中提供更高的生产力和更深的眼界。

 

 

Since the Web 2.0 revolution, making
content rich applications within

minutes is no more illusion. Android has
brought developing to unknown

speeds. Let me provide an example:

Someday I stumbled over the Buzzword
‘DrivingDirections’ within the

Android-Documentation.

由于Web2.0的革命,在短时间内构建内容丰富的应用程序不再是幻想。Android给开发带来了无止尽的速度。有一天,我偶然在Android的文档里发现了一个时髦的术语'DrivingDirections

 Thought
– done.

Picture 2 Google DrivingDirections
implementation in Android

 

The development process of the application
in the picture above took

about 1. hours! (Including the simple user
interface and all images you

see). Could one create such a sophisticated
application on any other

mobile-platform? – No.

该图中的应用程序开发过程需约1.5个小时(包括简单的用户界面和所有的你看到的图片)。

你能够在其他的移动平台上在这么短的时间内建立一个如此复杂的程序么?不可能吧。


 

Additionally this application could be
enriched with the current GPS-

position of the device within a handful of
code-lines.

此外,这个应用可以用当前设备的GPS来丰富,只需要简单地手写几行代码。

Google emphasizes Androids power of
providing location-based-services.

Google Maps are so neat within Android as
if it was just developed for

Android. One can integrate a fully zoom and
drag enabled map by adding

just 3(!) characters in the Java-Code of
the Android-Default-Application

and 3 lines of XML-Code.

Google 强调Android基于位置服务的权限。Google地图在Android里应用如此容易仿佛它就是为了Android而开发的。人们可以完全集成地图的变焦和拖动只用在默认应用里添加3java代码和3xml代码。

Other nice features that are easy to use
with Android are Animations and

media-playback. Since version m5, the
Android SDK contains functions

for straight and reverse GeoCoding and in
addition to mp3, playback of:

ogg-Vorbis, MIDI and a bunch of other
formats.

其他一些用Android容易实现的简单功能有动画和媒体播放,从M5版本开始,Android SDK包含了为正反地理编码和支持mp3播放 ogg-Vorbis,MIDI以及其他一系列播放格式。

 

The first weeks

最初的几周

Unfortunately the developers had to deal
with a not fully implemented

preview-SDK (initially released build:
‘m3-rc20’), where even some key-

parts of the SDK were poorly documented.
The lack of documentation

leaded to an explosion of the
Android-Developer-Group within the

Google-Groups.

不幸的是,在最初的几周,开发者不得不面对没有完全实施的SDK前置版本(最初的释放版’m3-rc20’,在里面甚至一些SDK的关键部分描述的仍然不够广泛。缺少文档导致了Google团队里的Google开发者团队的爆炸。

Sooner or later one had to respect the
statement from

Google:

If it is not documented
it is not meant to work yet.“

Many developers did not realize that fact
that the first SDK released were

first-looks or developer-previews, where
changes in the API had to be

awaited.

迟早,程序员必须面对Google造成的这一情景,如果没有充分的文档,是不是就意味着不要工作呢。

许多开发者还没有意识到这样的一个事实,第一个发布的sdk只是一个第一概貌或者说只是一个开发者的前序预览,还有大量API的改变需要我们等待。

Another annoying bug was the choppy
emulator-sound, which was said

fixed with the release of build ‘m3-rc37a’
about 4 weeks later, but still

happened on some setups up to m5.

另一个恼人的错误是波涛汹涌的仿真器声,据说会在‘m3-rc37a’版本中也就是4周之后修正,但是在m5安装以后这种错误依然存在。

 

Dalvik.equals(Java) == false

Dalvikjava不相等

Why “Dalvik”? – The Dalvik virtual machine
was named by Bornstein after

the fishing village of Dalvík in
Eyjafj.reur (Iceland), where some of his

ancestors lived.

为什么叫Dalvik- Dalvik虚拟机是由Bornstein根据冰岛的小渔村Dalvik村命名的,他的祖先曾居住在那里。

As you may have heard of, Dalvik is the
name of Android's virtual

machine. It is an interpreter-only virtual
machine that executes files in

the Dalvik Executable (*.dex) format, a
format that is optimized for

efficient storage and memory-mappable
execution. The virtual machine

is register-based, and it can run classes
compiled by a Java language

compiler that have been transformed into
its native format using the

included "dx" tool. The VM runs
on top of a Linux 2.6 kernel, which it

relies on for underlying functionality
(such as threading and low level

memory management). The DalvikVM was also
optimized to be running

in multiple instances with a very low
memory-footprint. Several VMs

protect ones application from being dragged
down by another crashed

Application.

正如你已经听说过,DalvikAndroid的虚拟机。它是一个解释执行的虚拟机,它执行Dalvik可执行文件(*.dex)格式,一个对有效存储和内存填涂执行优化的格式。这个虚拟机是基于寄存器的,并且它可以执行由java语言编译的类。编译器包含“dx”工具已经被转化为本地格式。虚拟机在Linux 2.6版本内核上运行。它依靠一些基本的功能(比如线程和低水平内存管理)。这个Dalvik虚拟机还优化运行多个实例仅仅使用一个非常低的内存足迹。几个虚拟机保护一些应用防止他们被其他突然崩溃的应用所毁坏。

Differences to a normal JavaVM

与一个正常的java虚拟机之间的区别。

JavaVM’s one can find on almost any desktop
computer nowadays are

Stack-based Virtual Machines (VM).The
DalvikVM on the other hand is

register based, because on
mobile-processors are optimized for register-

based execution. Also of register-based VMs
allow faster execution times

at the expense of programs which are larger
after compilation.

Java虚拟机可以在几乎任何桌面电脑现在是
基于堆栈的虚拟机(VM)中找到。在另一方面,Dalvik虚拟机基于寄存器的,因为在移动处理器上,采用继续寄存器的执行更加优化。同样,基于移动处理器虚拟机允许更快的执行时间,但是在编译之后程序也会更大。

 


 

The Android Code Challenge

Android的编程挑战

The Android Code Challenge (ADC) is an idea
of Google to encourage the

community to build cool applications for
the Android Platform by

rewarding the 50 most promising
applications submitted.

Android编程挑战是GoogleAndroid平台设立的为了鼓励开发人员一起来创新更酷平台的一个想法。

 

Picture 3 Android Developer Challenge Logo

Of course the Android Developers Challenge,
with its overall 10 Million

Dollars of prize money, was attracting even
more software-developers to

create a bunch of really useful
applications. On the other side many

voices said, that this was no good choice
from Google, because it would

lead to less code-sharing, as many would
fear sharing their ideas with a

community, during the first important
months after the SDK release.

当然,Android开发者大赛,因为有整整1千万美元的奖励,所以它吸引了更多软件开发人员去开发一些真正有用的应用程序。在另一方面,也有人说,这是因为Google没有更好的选择,因为这将会导致更少的代码共享,因为很多人担心一个开发者社区在第一个重要的SDK发布之后会分享他们的思想。

There were two Challenges planned:

计划中比赛分为两个阶段:

. Android Developer Challenge I:
Submissions up to April 14, 2008

. Android Developer Challenge II: This part
will launch after the

first handsets built on the platform become
available in the

second half of 2008.

第一阶段:2008314日之前,提交建议书。

第二阶段:在2008年下半年,需要提交能在平台上运行的应用。

 

 

In the Android Developer Challenge I, the
50 most promising entries

submitted by April 14 will each receive a
$25,000 award to fund further

development. Those selected will then be
eligible for even greater

recognition via ten $275,000 awards and ten
$100,000 awards.

Applications submitted to the Challenge
were supposed to be innovative

and demonstrate all the capabilities of the
Android platform, like location

based services, media consumption, gaming
and social networking, to

enrich mobile experience.

在第一阶段,50个最有希望的参赛方案将收到25000美元奖励资金作为进一步发展之用。这些被选中的作品将有可能获得更大的275000美元和10万美元的奖励。参加比赛的应用程序应该有创新性并且能够在Android平台上进行使用,比如基于未知的服务,媒体消费,游戏和社交,来获得丰富的移动体验。

 


 

Creating Android Applications

创建Android应用程序

In this chapter we will concentrate on the
specialties in creating any

Android-Application.

在这章之中,我们将会集中在如何创建Android应用程序。

Anatomy of an Android Application

解剖Android应用

There are four building blocks to an
Android application:

 

. Activity

. Intent Receiver

. Service

. Content Provider

 

Android应用有四个主要的构建块:

活动

意向收集机

服务

内容提供

Not every application needs to have all
four, but your application will be

written with some combination of these.

不是每个应用程序都拥有以上四种模块,但是您的应用程序需要把其中的一些结合起来。

Once you have decided what components you
need for your application,

you should list them in a file called
AndroidManifest.xml. This is an

XML file where you declare the components
of your application and what

their capabilities and requirements are. We
will discuss soon, what the

AndroidManifest.xml is responsible for.

一旦决定您的应用程序需要哪些组件,您应该在一个名为 AndroidManifest.xml 的文件中对其进行列表声明。这是一个声明应用程序组件的功能和需求的XML文件。我们很快就会谈到,AndroidManifest.xml是做什么用的。

 

Activity

活动

Activities are the most common of the four
Android building blocks. An

activity is usually a single screen in your
application. Each activity is

implemented as a single class that extends
the Activity base class. Your

class will display a user interface
composed of Views and respond to

events.

活动4Android构造块中最常见的,一个活动通常就是在应用程序中的一个单独的界面。

每一个活动都是作为一个独立的类从活动的基类扩展实现。您的类将显示由视图组成用户界面并且响应事件。

Most applications consist of multiple
screens. For example, a text

messaging application might have one screen
that shows a list of

contacts to send messages to, a second
screen to write the message to

the chosen contact, and other screens to
review old messages or change

settings. Each of these screens would be
implemented as an activity.

Moving to another screen is accomplished by
a starting a new activity. In

some cases an Activity may return a value
to the previous activity - for example an activity that lets the user pick a
photo would return the chosen photo to the caller.

When a new screen opens, the previous
screen is paused and put onto a

history stack. The user can navigate
backward through previously opened

screens in the history. Screens can also
choose to be removed from the

history stack when it would be
inappropriate for them to remain.

Android retains history stacks for each
application launched from the

home screen.

 

大多数应用程序由多个页面组成。例如一个文本消息的应用程序不要有一个页面显示一列联系人来发送消息,第二个页面来写消息给选中的联系人,还有一个页面来查看旧的消息或者更改设置。每一个这样的页面都被新开的一个活动来完成。在一些情况下,一个活动可能返回一个值给前一个活动。比如说一个活动让用户选择一个照片,将会返回选定的照片给调用者。当一个新的页面打开时,前一页被暂停并且放入一个历史堆栈里。用户可以向后浏览历史记录中打开过的页面。当页面不适合继续留在堆栈中时,也可以从历史栈中删除。

Android保留从主屏幕起启动的所有程序在历史栈中。

Intent and Intent Filters

IntentIntent筛选器

Android uses a special class called Intent
to move from screen to

screen. Intent describe what an application
wants done. The two most

important parts of the intent data
structure are the action and the data

to act upon. Typical values for action are
MAIN (the front door of the

application), VIEW, PICK, EDIT, etc. The
data is expressed as a Uniform

Resource Indicator (URI). For example, to
view a website in the browser,

you would create an Intent with the VIEW
action and the data set to a

Website-URI.

Android用一个特殊类从一个页面跳转到另外一个页面。Intent描述了一个应用程序想要做的事。Intent数据结构最重要部分是动作和接下来要用到的数据。典型的动作值就是MAIN(应用程序入口函数),VIEW,PICK,EDIT等等。这些数据用统一资源标识符URI来表示。

new
Intent(android.content.Intent.VIEW_ACTION,

ContentURI.create("http://anddev.org"));

 

There is a related class called an
IntentFilter. While an intent is

effectively a request to do something, an
intent filter is a description of

what intents an activity (or intent
receiver, see below) is capable of

handling. An activity that is able to
display contact information for a

person would publish an IntentFilter that
said that it knows how to

handle the action VIEW when applied to data
representing a person.

Activities publish their IntentFilters in
the AndroidManifest.xml

file.

有一个相关的类叫做IntentFilter。当一个Intent发出一个请求来做某事时,一个

当一个Intent发出某种请求时,一个IntentFilter是描述什么是这个活动需要处理的意图。

一个能够对一个人显示联系人信息的活动会发布一个IntentFilter表示它知道如何处理当代表一个人的操作数据的VIEW的这个动作。活动把他们的IntentFilters声明在AndroidManifest.xml 文件中。

 

Navigating from screen to screen is
accomplished by resolving intents. To

navigate forward, an activity calls
startActivity(myIntent). The

system then looks at the intent filters for
all installed applications and

picks the activity whose intent filters
best matches myIntent. The new

activity is informed of the intent, which
causes it to be launched. The

process of resolving intents happens at run
time when startActivity is

called, which offers two key benefits:

从页面到页面的导航通过载入intents实现。为了载入下一个页面,一个活动调用函数startActivity(myIntent)。系统将会从相应的intent filters中找出安装了的应用并且选在这些intent
filters
最适合自己Intent的应用。这个新的活动在这个intent中声明并启动。解析intents的过程发生在startActivity调用时,提供了两个比较方便的应用:

 

. Activities can reuse functionality from
other components simply

by making a request in the form of an
Intent

. Activities can be replaced at any time by
a new Activity with an

equivalent IntentFilter

活动可以重复使用其他组件的功能,仅需要通过请求其中的一个目的形式。

活动还可以在任何时候与一个等效的IntentFilter进行替换。

 

Intent Receiver

Intent接收器

You can use an IntentReceiver when you want
code in your

抱歉!评论已关闭.