版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 Application Fundamentals Android applications are written in the Java programming language. The compiled Java code along with any data and resource files required by the application is bundled by the aapt tool into an Android package, an archive file marked by an .apk suffix. This file is the vehic
2、le for distributing the application and installing it on mobile devices; it's the file users download to their devices. All the code in a single .apk file is considered to be one application. Android应用基础:Android应用程序是通过java语言开发的,通过绑定一些应用所需要的东西,例如:编译的Java代码,加上数据和一些资源文件,使用一个apt的工具将所有的东西封装成一个android
3、包,这个文件的文件后缀是.apk。这个文件是分发并安装应用程序到移动设备的载体,是用户获得该应用程序所需要的下载的文件。Application Components A central feature of Android is that one application can make use of elements of other applications (provided those applications permit it). For example, if your application needs to display a scrolling list of images
4、 and another application has developed a suitable scroller and made it available to others, you can call upon that scroller to do the work, rather than develop your own. Your application doesn't incorporate the code of the other application or link to it. Rather, it simply starts up that piece o
5、f the other application when the need arises. For this to work, the system must be able to start an application process when any part of it is needed, and instantiate the Java objects for that part. Therefore, unlike applications on most other systems, Android applications don't have a single en
6、try point for everything in the application (no main() function, for example). Rather, they have essential components that the system can instantiate and run as needed. There are four types of components: Activities An activity presents a visual user interface for one focused endeavor the user can u
7、ndertake. For example, an activity might present a list of menu items users can choose from or it might display photographs along with their captions. A text messaging application might have one activity that shows a list of contacts to send messages to, a second activity to write the message to the
8、 chosen contact, and other activities to review old messages or change settings. Though they work together to form a cohesive user interface, each activity is independent of the others. Each one is implemented as a subclass of the Activity base class. An application might consist of just one activit
9、y or, like the text messaging application just mentioned, it may contain several. What the activities are, and how many there are depends, of course, on the application and its design. Typically, one of the activities is marked as the first one that should be presented to the user when the applicati
10、on is launched. Moving from one activity to another is accomplished by having the current activity start the next one. Android有四大应用程序组件Android的一个很重要的中心特征就是一个应用程序能充分利用其他的应用程序的一些组件(前提是被允许的) 例如:如果的当前开发的应用需要一个滚动的列表去展示相片并且当时其他的程序已经开发了一个合适的滚动列表并且对其他人可用,你可以调用这个滚动的列表来完成你的工作而不是开发你自己的。你的应用不需要包含那个应用的代码或来链接它。相反
11、,它只是简单的在你需要它是启动这部分的应用为了实现这部分的功能,当前系统必须能启动其他应用程序进程的功能当那些部分它需要时,并且为这部分的java对象初始化这个java对象。因此,不想在其他系统中的应用程序,android应用程序不需要一个单一的入口点(例如:没有main()而是,提供了实例化和运行所需的必备组件。Android有四大应用程序组件1:activity一个Activity基类是为展示可视化用户接口。例如:一个Activity可以显示一个供用户选择的菜单列表,或者可以展示一些图片以及对应的说明。一个短信的应用有一个活动显示发送消息的一系列的联系人,第二个活动去编写信息文本去选择联系
12、人,第三个活动去查看以前的信息或修改一些设置。虽然它们组成了一个内聚的用户界面,每一个活动都和其他的的相互独立,每一个都继承Activity,是Activity的子类。一个应用程序可以只有一个Activity,但是也可以有多个,比如刚刚提到的文本应用。每个程序的作用,有多少个程序,取决于该应用的设计。ServicesA service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time. For example, a servic
13、e might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it. Each service extends the Service base class. 2:services服务是android中一个在后台运行的应用程序,没有可视化的用户界面。例如:你可以在使用一个用户程序的同时播放背景音乐。并且此
14、时,播放音乐的代码就不需要和用户交互,因此可以作为一个服务来运行,并且使用用户服务的接口来实现音乐的播放,暂停,等功能。对于不用向用户展示用户界面的情况下,使用服务是一个理想的选择。如同其他的组件一样,services运行于应用程序进程的主线程内。所以它不会对其他组件或用户界面有任何的妨碍,他们一般会派生一个新线程来执行一些时间消耗型的任务。Broadcast receivers A broadcast receiver is a component that does nothing but receive and react to broadcast announcements. Many
15、 broadcasts originate in system code for example, announcements that the time zone has changed, that the battery is low, that a picture has been taken, or that the user changed a language preference. Applications can also initiate broadcasts for example, to let other applications know that some data
16、 has been downloaded to the device and is available for them to use. An application can have any number of broadcast receivers to respond to any announcements it considers important. All receivers extend the BroadcastReceiver base class. Broadcast receivers do not display a user interface. However,
17、they may start an activity in response to the information they receive, or they may use the NotificationManager to alert the user. Notifications can get the user's attention in various ways flashing the backlight, vibrating the device, playing a sound, and so on. They typically place a persisten
18、t icon in the status bar, which users can open to get the3:Broadcast receiverbroadcast receiver是一个与注于接收广播通知信息并做出相应处理的组件。许多广播是由系统代码产生的例如通知时区改变、电池电量低、拍摄了一张照片或者用户改变了语言选项。应用程序也可以发起广播例如通知其它应用程序一些数据已经下载到设备上并处于可用状态。 一个应用程序可以拥有任意数量的broadcast receiver以对所有它认为重要的通知信息予以响应。所有的receiver均继承自BroadcastReceiver基类。 bro
19、adcast receiver没有用户界面。然而它们可以启动一个activity来响应它们收到的信息或者也可以使用NotificationManager来通知用户。通知可以用多种方式来吸引用户的注意力闪动背光灯、震动设备、播放声音等等。通知一般是在状态栏上放一个持续的图标用户可以打开它并获取消息。Content providers A content provider makes a specific set of the application's data available to other applications. The data can be stored in the
20、file system, in an SQLite database, or in any other manner that makes sense. The content provider extends the ContentProvider base class to implement a standard set of methods that enable other applications to retrieve and store data of the type it controls. However, applications do not call these m
21、ethods directly. Rather they use a ContentResolver object and call its methods instead. A ContentResolver can talk to any content provider; it cooperates with the provider to manage any interprocess communication that's involved. See the separate Content Providers document for more information o
22、n using content providers. Whenever there's a request that should be handled by a particular component, Android makes sure that the application process of the component is running, starting it if necessary, and that an appropriate instance of the component is available, creating the instance if
23、necessary. 4:内容提供者content providercontent provider将一些特定的应用程序数据供给其它应用程序使用。数据可以存储于文件系统、SQLite数据库或其它有意义的方式。content provider继承于ContentProvider 基类实现了一套使得其他应用程序能够检索和存储它所管理类型数据的标准方法。然而应用程序并不直接调用返些方法而是使用一个 ContentResolver 对象调用它的方法作为替代。ContentResolver可以与任何content provider进行会话与其合作对任何相关的进程间通讯进行管理。 参阅独立的Content
24、 Providers文档以获得更多关于使用content provider的信息。 每当出现一个需要被特定组件处理的请求时Android会确保那个组件的应用程序进程处于运行状态必要时会启动它并确保那个组件的一个合适的实例可必要时会创建那个实例。Activating Components激活组件Three of the four component typesactivities, services, and broadcast receiversare activated by an asynchronous message called an intent. Intents bin
25、d individual components to each other at runtime (you can think of them as the messengers that request an action from other components), whether the component belongs to your application or another.四种类型组件中的三种:活动 服务和广播接受者都通过一个叫做intent的异步消息激活。这些intents在运行时(runtime)将这些属于你的程序或不同程序的单独的组件绑定在一起(bind),你可以把这
26、些intents看作是需要其他组件的action的messengers。An intent is created with an Intent object, which defines a message to activate either a specific component or a specific type of componentan intent can be either explicit or implicit, respectively.一个Intent通过一个Intent对象建立,该Intent对象定义了一个消息去激活一个特殊
27、的组件或一种特殊的组件-一个Intent既可以明确的说明也可以不明确,要分情况来看For activities and services, an intent defines the action to perform (for example, to "view" or "send" something) and may specify the URI of the data to act on (among other things that the component being started might need to know). For e
28、xample, an intent might convey a request for an activity to show an image or to open a web page. In some cases, you can start an activity to receive a result, in which case, the activity also returns the result in an Intent (for example, you can issue an intent to let the user pick a perso
29、nal contact and have it returned to youthe return intent includes a URI pointing to the chosen contact).对于活动和服务,一个Intent定义了一个动作去执行(比如:要View或者send什么)和要操作数据的uri,一个Intent可能传递了为一个Activity传递了一个请求去展示一张图片或者打开一个网页在这些例子中,你可以开始一个活动去接收结果,在这个例子中,这个Activity也可以返回这个通过一个Intent返回一个结果(例如,你可以发动一个Intent去让用户选择一个个人的conta
30、ct并且返回给你,这个返回的意图包括一个指向这个联系人的URI)For broadcast receivers, the intent simply defines the announcement being broadcast (for example, a broadcast to indicate the device battery is low includes only a known action string that indicates "battery is low").对于广播接收者来说,intent只是简单的定义了要广播的内容(比如,一个用以表明电
31、池电量很低的广播仅包含了一个表明电池电量很低的字符串)。The other component type, content provider, is not activated by intents. Rather, it is activated when targeted by a request from a ContentResolver. The content resolver handles all direct transactions with the content provider so that the component that's perform
32、ing transactions with the provider doesn't need to and instead calls methods on the ContentResolver object. This leaves a layer of abstraction between the content provider and the component requesting information (for security).另外一个组件的类型,内容提供者,不是通过意图激活。而是由接收到contentResolver请求是激活的这个内处理者
33、处理所有的直接的事务通过内容提供者。因此,There are separate methods for activating each type of component:这里有不同的方法来激活不同类型的组件· You can start an activity (or give it something new to do) by passing an Intent to startActivity() or startActivityForResult() (when you want the activity to r
34、eturn a result).· 你可启动一个一个活动(或者给他一些新的要做的内容)通过传递一个Intent来startActivity () 或者startActivityForResult()(当你需要这个Activity返回一个结果时)· You can start a service (or give new instructions to an ongoing service) by passing an Intent to startService(). Or you can bind to the service by pass
35、ing an Intent tobindService().你可以启动一个Service(或者给一个内在的服务一些新的指令)通过传递一个Intent来startServices(),或者你可以通过把一个Intent传递给bindService()来绑定一个service。· You can initiate a broadcast by passing an Intent to methods like sendBroadcast(), sendOrderedBroadcast(), or sendStickyBr
36、oadcast().你可以通过传递一个Intent给诸如sendBroadcast()、sendOrderedBroadcast()或者sendStickyBroadcast()等方法来初始化一个广播。 · You can perform a query to a content provider by calling query() on a ContentResolver.你可以通过调用ContentResolver的query()方法来执行一次content provider的查询操作。·For more information
37、 about using intents, see the Intents and Intent Filters document. More information about activating specific components is also provided in the following documents: Activities, Services, BroadcastReceiver and Content Providers. 更多关于使用Intent的信息,查看Intent和IntentFilte
38、rs文档。更多关于如何激活特定的组件同样在下面的文档中提供:Activities, Services, BroadcastReceiver and Content Providers. Declaring components声明组件The primary task of the manifest is to inform the system about the application's components. For example, a manifest file can declare an activity as follows:Ma
39、nifest的首要的任务就是声明系统中有关这个应用的组件。例如,一个manifest文件可以通过声明一个Activity如下:<?xml version="1.0" encoding="utf-8"?><manifest . > <application android:icon="drawable/app_icon.png" . > <activity android:name="com.example
40、.project.ExampleActivity" android:label="string/example_label" . > </activity> . </application></manifest>In the <application&g
41、t; element, the android:icon attribute points to resources for an icon that identifies the application.在这个<application>元素中android:icon属性指向一个资源(用来表示当前这个应用程序的图标)In the <activity> element, the android:name attribute specifies the fully qualified class name
42、 of the Activity subclass and the android:label attributes specifies a string to use as the user-visible label for the activity.在<Activity> 这个元素中,android :name属性用于确定继承Activity的的子类的全路径名android:label属性用于表示这个用户可见的标签 You must declare all application components this way:你必须通过一下的
43、方法来声明这些应用组件l <activity> elements for activitiesl <service> elements for servicesl <receiver> elements for broadcast receiversl <provider> elements for content providersactivities:<activity>标签services:<service>标签broadcast receiver:<recei
44、ver>标签content providers:<provider>标签Activities, services, and content providers that you include in your source but do not declare in the manifest are not visible to the system and, consequently, can never run. However, broadcast receivers can be either declared in the manifest or crea
45、ted dynamically in code (as BroadcastReceiver objects) and registered with the system by calling registerReceiver().如果你的程序中用到了活动,服务,内容提供者但是没有在manifest中声明,结果是这些组件不会被系统所知道从而不会运行。但是,broadcast Receivers 既可以在manifest中声明也可以动态的通过代码来创建(作为broadcast Receivers对象)并且使用registerReceiver()在系统中注册 Decl
46、aring component capabilities声明组件的能力As discussed above, in Activating Components, you can use an Intent to start activities, services, and broadcast receivers. You can do so by explicitly naming the target component (using the component class name) in the intent. However, the real powe
47、r of intents lies in the concept of intent actions. With intent actions, you simply describe the type of action you want to perform (and optionally, the data upon which youd like to perform the action) and allow the system to find a component on the device that can perform the action and start it. I
48、f there are multiple components that can perform the action described by the intent, then the user selects which one to use.就像上面说的那样,在激活组件的时候,你可以使用Intent来开始活动,服务,内容提供者。你可以明确的在Intent中声明目标组件的名称(使用组件的类名)。但是,Intent的真正的能力取决于Intent的action的概念,你可以简单的描述你要操作的动作的类型(或者是有选择的描述你要的在动作中使用的数据),并且可以允许系统找到一个在设备上组件执行和启
49、动它。如果有多个在Intent中描述能执行动作的组件,则可以让用户去选择自己想用的The way the system identifies the components that can respond to an intent is by comparing the intent received to the intent filters provided in the manifest file of other applications on the device.系统识别能对Intent做出响应的方式是通过比较接收到的Intent和设备中应用程序的manifes
50、t文件中的Intent filtersWhen you declare a component in your application's manifest, you can optionally include intent filters that declare the capabilities of the component so it can respond to intents from other applications. You can declare an intent filter for your component by adding an <
51、;intent-filter> element as a child of the component's declaration element.当你在应用的manifest中声明一个组件时,你可以有选择的包含Intent filters,这些Intent filters表明了这些组件对其他应用程序的Intent做出反应的能力。你可以通过添加一个<intent-filter>作为 来为你的组件声明一个Intent filtersFor example, an email application with an activity for composing
52、a new email might declare an intent filter in its manifest entry to respond to "send" intents (in order to send email). An activity in your application can then create an intent with the “send” action (ACTION_SEND), which the system matches to the email applications “send” activity and lau
53、nches it when you invoke the intent with startActivity().例如:一个有处理新邮件Activity的邮件应用也许可以声明一个Intent filter在它的manifest入口中来作为send Intent(为了send Email ),在你的应用中可以创建一个带有发送的Intent(action_send),当你用startactivity()调用这个Intent,系统在邮件程序中匹配一个send的Activity并且运行它For more about creating intent filters, see the
54、Intents and Intent Filters document.Declaring application requirements声明运行程序所需要的条件There are a variety of devices powered by Android and not all of them provide the same features and capabilities. In order to prevent your application from being installed on devices that lack features needed by y
55、our application, it's important that you clearly define a profile for the types of devices your application supports by declaring device and software requirements in your manifest file. Most of these declarations are informational only and the system does not read them, but external services suc
56、h as Google Play do read them in order to provide filtering for users when they search for applications from their device.有多种多样的设备上运行着android系统并且不是所有的设别都提供相同的特征和能力,为了防止你的程序安装在缺乏相应功能的设备上,在你的manifest未见中声明硬件和软件的要求(为了让你的应用被不同的设备支持)变得尤为的重要。大多数这些信息和声明并不会被系统读取。但是其他的服务比如Android Market却会阅读这些声明来帮助通过通过自己的
57、设备搜索软件的用户过滤软件。For example, if your application requires a camera and uses APIs introduced in Android 2.1 (API Level 7), you should declare these as requirements in your manifest file. That way, devices that do not have a camera and have an Android version lower than 2.1 cann
58、ot install your application from Google Play.比如:你的应用需要照相和android 2.1的api,你应该在你的manifest文件中声明这些要求。通过这种方法,这些没有相机功能或者是android版本低于2.1的不能冲Google play中安装该应用However, you can also declare that your application uses the camera, but does not require it. In that case, your application must perform a
59、check at runtime to determine if the device has a camera and disable any features that use the camera if one is not available.然而,你也可以在你的应用中使用相机,但是并不是需要它。在这个情况下,你的应用在运行时必须执行一次检查来确定这个设备是否有相机。如果你的设备没有相机,那么系统会使使用照相机的相关程序不可用Here are some of the important device characteristics that you should consider as
60、 you design and develop your application:在你设计和开发你的应用程序的时候,这里是一些你应该考虑的重要的设备特征Screen size and density屏幕尺寸和分辨率In order to categorize devices by their screen type, Android defines two characteristics for each device: screen size (the physical dimensions of the screen) and screen density (the physical de
61、nsity of the pixels on the screen, or dpidots per inch). To simplify all the different types of screen configurations, the Android system generalizes them into select groups that make them easier to target.为了对屏幕类型进行分类。Android为每个设备定义了2个重要的特征:屏幕大小和屏幕分辨率。为了简化所有屏幕配置的不同类型,android系统把他们集中到一起去选择来更好的去瞄准The screen sizes are: small, normal, large, and extra large.The screen densities are: low density, medium density, high density, and extr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论