Intro/anatomy

From Android中文网

Android中文网(androidcn.net) 版权申明 : creativecommons licenses
Jump to: navigation, search

目录

[编辑] 解析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. See the Android manifest file documentation for complete details.

并不是每个程序都需要所有的四个部分,你的程序可能用到它们其中一些的组合。

一旦你决定在你的程序中需要哪个组件,你应该在一个叫AndroidManifest.xml的文件中列出它们。你在这个XML文件中声明程序中用到的组件和它们的用途和需要。在Android manifest file 文档中查阅完整的细节。

[编辑] 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. 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.

Activity是Android构造块中最基本的一种,在应用中,一个activity通常就是一个单独的屏幕。 每一个activity都被实现为一个独立的类,并且继承于Activity这个基类。这个activity类将会显示 由几个Views控件组成的用户接口,并对事件做出响应。大部份的应用都会包含多个的屏幕。例如, 一个短消息应用程序将会有一个屏幕用于显示联系人列表,第二个屏幕用于写短消息,同时还会有用 于浏览旧短消息及进行系统设置的屏幕。每一个这样的屏幕,就是一个activity。从一个屏幕导航到 另一个屏幕是很简单的。在一些应用中,一个屏幕甚至会返回值给前一个屏幕。

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

Android uses a special class called an Intent to move from screen to screen. An intent describes 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 activity), VIEW, PICK, EDIT, etc. The data is expressed as a URI. For example, to view contact information for a person, you would create an intent with the VIEW action and the data set to a URI representing that person.

Android使用了Intent这个特殊类,实现在屏幕与屏幕之间移动。Intent类用于描述一个应用想要做什么事。 在Intent的描述结构中,有两个最重要的部分:动作和动作对应的数据。典型的动作类型有:MAIN(activity的门 户)、VIEW、PICK、EDIT等。而动作对应的数据则以URI的形式进行表示。例如:要查看一个人的联系方式,你需要 创建一个动作类型为VIEW的intent,以及一个表示这个人的URI。

Android利用一个叫Intent的特殊类来实现屏幕间的切换。一个Intent描述了一个应用程序想要做什么。intent数据结构中两个最重要的部分是动作和动作对应的数据。一个典型动作的值是MAIN(活动的入口),VIEW,PICK,EDIT,和其它一些。数据表示为URI。例如,浏览一个人的联系信息,你应该创建一个带有VIEW活动的intent,并且URI的数据集指向那个人。

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 filter则用于 描述一个activity(或者Intent Receiver)能够操作哪些intent。一个activity如果要显示一个人的联系方式时, 需要声明一个IntentFilter,这个IntentFilter要知道怎么去处理VIEW动作和表示一个人的URI。IntentFilter需 要在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:

一个屏幕到另一个屏幕之间的导航是通过解析意图(Intent)来实现的。当向前导航时,activity将会调用startActivity (Intent myIntent)方法。然后,系统会在所有安装的应用程序中定义的IntentFilter中查找,找到最匹配myIntent的 Intent对应的activity。新的activity接收到myIntent的通知后,开始运行。当startActivity方法被调用将触发解析 myIntent的动作,这个机制提供了两个关键好处:

  • 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
  • Activity能够简单的通过在Intent表中发送请求,从其他组件中复用功能
  • Activity能够在任何时候由一个带有相同IntentFilter的Activity替换

[编辑] Intent Receiver

You can use an IntentReceiver when you want code in your application to execute in reaction to an external event, for example, when the phone rings, or when the data network is available, or when it's midnight. Intent receivers do not display a UI, although they may use the NotificationManager to alert the user if something interesting has happened. Intent receivers are registered in AndroidManifest.xml, but you can also register them from code using Context.registerReceiver(). Your application does not have to be running for its intent receivers to be called; the system will start your application, if necessary, when an intent receiver is triggered. Applications can also send their own intent broadcasts to others with Context.broadcastIntent().

当你希望你的应用能够对一个外部的事件(如当电话呼入时,或者数据网络可用时,或者到了晚上时)做出响应, 你可以使用一个Intent Receiver。虽然Intent Receiver在感兴趣的事件发生时,会使用NotificationManager 通知用户,但它并不能生成一个UI。Intent Receiver在AndroidManifest.xml中注册,但也可以在代码中使用 Context.registerReceiver()进行注册。当一个intent receiver被触发时,你的应用不必对请求调用intent receiver,系统会在需要的时候启动你的应用。各种应用还可以通过使用Context.broadcastIntent()将它们自己的 intent receiver广播给其它应用程序。

[编辑] Service

A Service is code that is long-lived and runs without a UI. A good example of this is a media player playing songs from a play list. In a media player application, there would probably be one or more activities that allow the user to choose songs and start playing them. However, the music playback itself should not be handled by an activity because the user will expect the music to keep playing even after navigating to a new screen. In this case, the media player activity could start a service using Context.startService() to to run in the background to keep the music going. The system will then keep the music playback service running until it has finished. (You can learn more about the priority given to services in the system by reading Lifecycle of an Android Application.) Note that you can connect to a service (and start it if it's not already running) with the Context.bindService() method. When connected to a service, you can communicate with it through an interface exposed by the service. For the music service, this might allow you to pause, rewind, etc.

一个Service是一段长生命周期的,没有用户界面的程序。比较好的一个例子就是一个正在从播放列表中播放歌曲的媒 体播放器。在一个媒体播放器的应用中,应该会有多个activity,让使用者可以选择歌曲并播放歌曲。然而,音乐重放这个 功能并没有对应的activity,因为使用者当然会认为在导航到其它屏幕时音乐应该还在播放的。在这个例子中,媒体播放器 这个activity会使用Context.startService()来启动一个service,从而可以在后台保持音乐的播放。同时,系统也将 保持这个service一直执行,直到这个service运行结束。另外,我们还可以通过使用Context.bindService()方法,连接 到一个service上(如果这个service还没有运行将启动它)。当连接到一个service之后,我们还可以service提供的接口 与它进行通讯。拿媒体播放器这个例子来说,我们还可以进行暂停、重播等操作。

[编辑] Content Provider

Applications can store their data in files, an SQLite database, or any other mechanism that makes sense. A content provider, however, is useful if you want your application's data to be shared with other applications. A content provider is a class that implements a standard set of methods to let other applications store and retrieve the type of data that is handled by that content provider.

To get more details on content providers, see Accessing Content Providers.

应用程序能够将它们的数据保存到文件中、SQL数据库中,甚至是任何有效的设备中。当你想将你的应用数据与其它的 应用共享时,Content Provider将会很有用。一个Content Provider类实现了一组标准的方法,从而能够让其它的应用 保存或读取此Content Provider处理的各种数据类型。

更详细的Content Provider资料,可以参考附带文档中的Accessing Content Providers。

Personal tools