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

Android应用程序剖析

2013年08月29日 ⁄ 综合 ⁄ 共 7160字 ⁄ 字号 评论关闭

  There are four building blocks to an Android application:

  Android应用程序保护4种构建模块:

  • Activity
  • Intent Receiver
  • Service
  • Content Provider

  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 documentation 有完整的细节描述信息。

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.

  活动是Android四个构建模块中最常用的一种。在你的应用中,一个活动通常是一个单独的画面。每一个活动通过一个继承了 Activity 基类的类实现。你的类将会显示一个有多个Views组成的用户界面,并响应一些事件。大部分应用包含多个画面。比如,一个文本信息应用将有一个画面用来显示要给其发送信息的联系人,第二个画面用来给选定的联系人写信息,另外一个画面用来回顾旧信息或修改设置。每一个这样的画面通过一个活动实现。切换到另一个画面通过启动一个新的活动来实现。在一些情况下,一个活动会返回一个值给前一个活动 -- 比如,一个活动让用户选一副图片会返回选中的图片给调用者。

  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

Intent和Intent过滤器

 

  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(活动的入口),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.

Navigating from screen to screen is accomplished by resolving intents. To navigate forward, an activity calls

t.Intent)">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:

  有一个相关的类IntentFilterIntent是一个要去做某些事情的请求,Intent过滤器是关于一个活动(或Intent接收器,看下面)能处理那些Intent的描述。一个可以显示联系人信息的活动可能会发布一个IntentFilter说当数据表示一个人时它知道如何处理VIEW动作。活动中AndroidManifest.xml文件中发布其IntentFilter,通过解析Intent来从一个画面切换到另一个画面。活动通过调用startActivity(myIntent). 方法来前移到新画面。系统在所有安装的应用的Intent过滤器中查找,并选择其Intent过滤器与myIntent最匹配的应用。intent被传递给新的活动,导致该活动被加载。解析Intents度过程在startActivity被调用时进行,这样有如下两个好处:

  • Activities can reuse functionality from other components simply by making a request in the form of an Intent
  • 通过构造一个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 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().

  当你希望你的应用在一个外部事件到达时执行时,可以用一个IntentReceiver ,比如,电话铃响,数据网络可用或时间已是午夜时分。Intent接收器不会显示一个用户界面,尽管它可能会用NotificationManager 来提示用户是否一些用户感兴趣的事情发生。Intent接收器在AndroidManifest.xml文件中注册,也可以在代码中通过Context.registerReceiver() 注册。你的应用并不需要处于运行状态来保证Intent接收器可以被调用;当一个Intent接收器被触发时,系统会在必要的时候启动你的应用。应用也可以将其自己的意图(Intent)通过Context.broadcastIntent()广播给其他应用。

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 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 /reference/android/content/Context.html#bindService(android.content.Intent,%20java.lang.String,%20android.content.ServiceConnection,%20int)">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.

  服务是没有用户界面,长期运行的代码。一个好的例子是媒体播放器播放列表中的歌曲。对于一个媒体播放器应用,可能会有一个或多个活动运行用户选择歌曲并播放,然而,并不会有一个活动来处理引用播放,因为用户可能期望在跳转到一个新画面时音乐继续播放。此时,媒体播放器活动会通过Context.startService()启动一个服务在后台运行来保持音乐持续播放。系统会一直播放音乐直到音乐结束。(通过读Lifecycle of an Android Application你可以了解更多关于系统中设置服务优先级的内容)。注意,你可以通过Context.bindService()方法连接到一个服务(如果服务没有运行,会启动服务)。当连接到一个服务后,你可以通过该服务暴露的接口与其进行通信。对于音乐服务,可能允许你暂定,倒带等。

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.

  应用可以通过文件,SQLite数据库或其它有意义的机制保存数据。如果你希望你的应用的数据被其他应用共享,一个内容提供者就会非常有用。一个内容提供者是一个实现了一组方法的类,可以让其他应用存储和获取被该内容提供者处理的数据。

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

  更多关于内容提供者的细节,请看Accessing Content Providers

抱歉!评论已关闭.