andorid 第四天 解析andoird app构造

here are four building blocks to an Android application:

Activity
Broadcast 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.



Android 应用程序大概包含如下四个模块。
Activity(不知道怎么翻译。)
内部广播接收者
服务
容器提供者


不是每一个应用程序都完全需要建立在这四个部分上,但是你的应用程序会使用到几个部分的组合。
当你决定了需要哪些部分来构建你的程序的时候,你需要把这些部分列出来并且写到一个叫做 AndroidManifest.xml的文件中。
这是一个XML文件(囧。。。),是一个你定义:你需要什么样的组成元件,以及他们都需要发挥什么作用和有什么需求的地方。
关于这个xml的详细文档请参阅:http://code.google.com/android/devel/bblocks-manifest.html

————————————————————————————————————————————————————
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.

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.

第一部分,Acitivity
Acitivity 是 Android 构建模块 中最公用的一个。一般来说,一个Activity就是你应用程序中的一个屏(界面?)
每一个Activity应该是一个独立的,继承于Activity 基类的类。
你的累应该显示一个由多个视图组成的用户界面,同时这个界面可以响应各种动作。


大多数的程序都包含很多屏。例子:一个SMS界面,也许应有个界面显示电话本,一个屏幕显示你要写的信息,一个屏幕显示老的信息或者用来修改设置。每一个屏幕都是通过activity来实现的。
切换到另一个屏幕的工作通过new一个activity来实现。也许有的时候一个activity会传递一些参数给下一个activity——举个例子,一个activity可以让用户来选择一个图片,然后把这个图片传给它的调用者。

当打开一个新屏的是或,前一个屏会暂停并放入“历史堆栈”中。
用户可以回到上一个存在历史堆栈中的打开的屏。
当然,在这些屏幕并不应该被留下的时候,我们可以把它们从历史堆栈中删除。
Android会保留每一个从Home 屏幕启动的应用程序历史。


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 application), 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.

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 BroadcastReceiver, 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 resolveing 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:

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

Intent 和 intent 过滤器。
Android用一个特别的叫做intent的类来切换屏幕。
intent 描述了一个应用程序将要做些什么。
intent最重要的两个数据结构部分,一个是动作(action),一个对动作起作用的数据(data to act upon,有疑议)。
典型的动作包括 MAIN(即程序的入口),VIEW,PICK,EDIT,等等(一些名词,我觉得不翻译比较好。)。数据则表示为URI。
举个例子,想要查看一个联系人的信息,你需要新建一个含有VIEW动作的intent,数据设置成一个URI来代表这个联系人(data set to a URI representing that person,有疑议)。

这里有一个与Intent有关的类叫做IntentFilter。在一个intent被要求做一个什么事情的时候,一个intent fliter将会说明,那种活动的intent(或者广播接收者,BroadcastReceiver)是可以被处理的。
一个可以显示联系人信息的activity将会公布一个IntentFilter,说明他知道当他接收到一个代表person的数据时,如何去处理一个 VIEW 动作。
Activities将会把他们的IntentFilters公布在AndroidManifest.xml文件中。

屏 与屏之间的导航通过intent来完成。如果需要回到之前一个叫做startActivity(或者myIntent)的activity页,系统会到 intent filter中查找所有已经安装好的applications中并且选取与myIntent最接近的那个activity。
新的activity接到intent的通知而启动,而解析intents在startActivity被调用的同时启动,这样做会有两个非常关键的好处

Activities 可以被其他的组件通过Intent的形式重新使用
Activites 可以随时被一个拥有相同Intentfilter的新activity替换。

(这一段总觉得翻译的有问题。。。)
(译者的话:关于这里的例子,可以参照SDK1.0中的ReciveResult.java和SendResult.java)

————————————————————————————————————————————————
Broadcast Intent Receiver
You can use a BroadcastReceiver 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. BroadcastReceivers do not display a UI,
although they may use the NotificationManager to alert the user if something interesting has happened. BroadcastReceivers 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 BroadcastReceivers to be called;
the system will start your application, if necessary, when a BroadcastReceiver is triggered.
Applications can also send their own intent broadcasts to others with Context.sendBroadcast().

内部广播接收者。
在你的applications运行响应一个外部动作的时候,你可以使用一个BroadcastReceiver(以下简称BR) 。
例如,当来电铃声响起,或者数据网络可用,或者在夜晚(暗处)。BR不会以一种UI的形式显示出来,他们会用一个NotificationManager来提醒用户有些他们感兴趣的事儿发生了。
BR注册在Androidmanifest.xml,当然你也可以把这些BR使用Context.registerReciver()注册到代码里。
你的application不必为了调用这些BR而启动,如果需要的话,系统会在一个BR被触发的时候直接启动你的application。
applications也可以通过Context.sendBroadcast()把他们的intent传递给其他的applications。

————————————————————————————————————————————————
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 Life Cycle 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服务。
一个服务是一段长时间存在的没有UI界面的code。一个不错的例子是播放器播放一个播放列表里面的音乐。
一个播放器里面会有一个或者多个activities来让用户选择和播放音乐。
但是,这个时候循环播放功能就无法被一个activity处理,因为用户希望在切换到下一个屏的时候音乐是继续播放的。
这种情况下,播放器activity就会通过Context.startService()来启动一个后台进程来继续播放音乐。
系 统会让音乐播放进程一直持续到整个播放表被播放完。(通过 Life Cycle of an Android Application[http://code.google.com/android/intro/lifecycle.html],你可以学习到更 多的有关于服务在系统中的优先级的知识。)
你可以通过Context.bindService()方法来连接到一个服务(如果这个服务没有启动,那么这个方法会启动这个服务。)
当连接到这个服务后,你可以通过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.

应用程序可以存储他们的数据到文件中,或者到一个SQLite数据库中,或者其他的办法存到哪里。
一个content provider,在你想要把你的应用程序数据分享给其他应用程序的时候是非常有用的。
(有一段没有翻译,确实没有理解。)
如果想得到更多的有关Content providers的信息,请参阅Accessing Content Providers。[http://code.google.com/android/devel/data/contentproviders.html]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值