今天開始學習google官網上的API guides ,主要讀了Application Fundamentals這一章節,此章節介紹了一個App的基本組成,共包括四大部份內容.html
1. Application Components(android應用程序的四大組件)java
2. Activating Components(下面描述的組件用於激活上述四大組件)android
3. The Manifest File(描述每一個app的組件構成,能力及對系統的要求)web
4. Application Resources(除了代碼之外,還有圖片/頁面佈局等其餘配置)api
下面逐一展開作記錄,詳細細節須要參考google官網http://developer.android.com/guide/components/fundamentals.htmlapp
Application Fundamentalsasync
1. Application Components(android應用程序的組件)ide
Android application包含四大組件:佈局
1) Activity: An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails.學習
NOTE: 一個application能夠包含若干個activity,其餘的app能夠獨立的調用本app的任意一個activity
2) Service: A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface. For example, a service might play music in the background while the user is in a different application
NOTE: Service即沒有UI的服務,能夠被start或bind
3) Content provider: A content provider manages a shared set of application data. You can store the data in the file system, an SQLite database, on the web, or any other persistent storage. Through the content provider, other applications can query or even modify the data (if the content provider allows it).
NOTE: Content Provider用來在不一樣app間共享數據
4) Broadcast receiver: A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcasts originate from the system—for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured. Applications can also initiate broadcasts—for example, to let other applications know that some data has been downloaded to the device and is available for them to use.
NOTE: Broadcast receiver沒有UI,可是能夠在status bar上建立alert通知用戶事件發生. Although broadcast receivers don't display a user interface, they may create a status bar notificationto alert the user when a broadcast event occurs.
2. Activating Components(下面描述的組件用於激活上述四大組件)
1) Intent: Three of the four component types—activities, services, and broadcast receivers—are activated by an asynchronous message called an intent
NOTE: activities, services, and broadcast receivers這三大組件是由intent事件激活(啓動),可是content provider不禁Intent驅動,見下一條
2) Content Resolver: Content provider, is not activated by intents. Rather, it is activated when targeted by a request from a ContentResolver
.
NOTE: The content resolver做爲中間層負責處理其餘組件與content provider之間的事務. The content resolver handles all direct transactions with the content provider so that the component that's performing transactions with the provider doesn't need to and instead calls methods on the ContentResolver
object.
對於四大組件的激活方式總結以下:
3. The Manifest File(描述每一個app的組件構成,能力及對系統的要求)
每一個app在其根目錄都放有一個文件AndroidManifest.xml
: 用來描述此app的組件構成以及需求等等. Android系統在經過讀取此manifest文件能夠了解本app的需求和功能.
Manifest 文件的主要做用以下
The manifest does a number of things in addition to declaring the application's components, such as:
1) Declaring components(用來聲明app所包含的組件)
You must declare all application components this way:
NOTE: Activities, services, and content providers 必須在Manifest中聲明,不然不可見,也不可運行. 可是Broadcast receiver既能夠在manifest中聲明,也能夠不在manifest中聲明而是動態向system註冊….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 created dynamically in code (as BroadcastReceiver objects) and registered with the system by calling registerReceiver().
2) Declaring component capabilities(聲明每一個組件所具備的能力,如能夠send,edit等…)
經過Intent Filter來聲明每一個組件的能力,也即app具備的能力(一個app具備若干個組件). 系統讀取每一個組件的intent filter字段,從而得知每一個組件的能力.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.
When 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.
NOTE: 定位程序入口activity的方法: 查找manifest,<intent-filter>中有 MAIN 和LAUNCHER的 activity就是
3) Declaring application requirements(用來聲明app對外界的需求)
Screen size and density: 對屏幕的尺寸和分辨率的要求,用<supports-screens>
字段來聲明
Input configurations:對輸入途徑的要求, 用字段 <uses-configuration>
來聲明
Device features:對硬件的要求如藍牙支持,相機支持,光感/重力感應器的支持等等,用字段 <uses-feature>
聲明
Platform Version: 要求的最低的android API level, 用 <uses-sdk>
字段聲
4. Application Resources(除了代碼之外,還有圖片/頁面佈局等其餘配置)
一個android的app除了java code之外,還須要包含其餘的資源.如程序所須要的圖片/動畫/音視頻/頁面佈局/不一樣語言/橫豎屏顯示調整等等. 這些儲存在app的res文件夾中.
An Android application is composed of more than just code—it requires resources that are separate from the source code, such as images, audio files, and anything relating to the visual presentation of the application. For example, you should define animations, menus, styles, colors, and the layout of activity user interfaces with XML files. Using application resources makes it easy to update various characteristics of your application without modifying code(不用調整代碼,只須要對應不一樣的配置文件就能夠) and—by providing sets of alternative resources—enables you to optimize your application for a variety of device configurations (such as different languages and screen sizes).