第二講:Android系統構架分析和應用程序目錄結構分析

本講內容:
Android系統構架簡介
Android應用程序結構分析

點這裏下載:Android學習指南第二講源代碼html

1、Android系統構架

Android系統從底向上一共分了4層,每一層都把底層實現封裝,並暴露調用接口給上一層。java

system-architecture

下面是簡單翻譯的版本:linux

system-architecture_nebulayao

  1. Linux內核(Linux Kernel)
    • Android運行在linux kernel 2.6之上,可是把linux內受GNU協議約束的部分作了取代,這樣在Android的程序能夠用於商業目的。
    • Linux 內核是硬件和軟件層之間的抽象層。
  2. 中間件
    • 中間件包括兩部分:核心庫和運行時(libraries & Android runtime)
    • 核心庫包括,SurfaceManager 顯示系統管理庫,負責把2D或3D內容顯示到屏幕;Media Framework 媒體庫,負責支持圖像,支持多種視頻和音頻的錄製和回放;SQlite 數據庫,一個功能強大的輕量級嵌入式關係數據庫;WebKit 瀏覽器引擎等。
    • Dalvik虛擬機:區別於Java虛擬機的是,每個Android 應用程序都在它本身的進程中運行,都有一個屬於本身的Dalvik 虛擬機,這一點可讓系統在運行時能夠達到優化,程序間的影響大大下降。Dalvik虛擬機並不是運行Java字節碼,而是運行本身的字節碼。
  3. 應用程序框架(Application Framework)
    • 豐富而又可擴展性的視圖(Views),能夠用來構建應用程序, 它包括列表(lists),網格(grids), 文本框(text boxes),按鈕( buttons), 可嵌入的web 瀏覽器。
    • 內容提供者(Content Providers)使得應用程序能夠訪問另外一個應用程序的數據(如聯繫人數據庫), 或者共享它們本身的數據。
    • 資源管理器(Resource Manager)提供非代碼資源的訪問,如本地字符串,圖形,和佈局文件( layoutfiles )。
    • 通知管理器(Notification Manager) 使得應用程序能夠在狀態欄中顯示自定義的提示信息。
    • 活動管理器( Activity Manager) 用來管理應用程序生命週期並提供經常使用的導航回退功能。
  4. 應用程序 (Applications)
    • Android 系統會內置一些應用程序包包括email 客戶端,SMS 短消息程序,日曆,地圖,瀏覽器,聯繫人管理程序等。全部的應用程序都是使用JAVA 語言編寫的。

2、Android應用程序結構分析

接下來讓我帶領你們解析一個Android程序的各個組成部分,此次咱們拿一個Hello,World作例子,雖然只是一個Hello,World,但也是麻雀雖小五臟俱全,經過分析Hello,World的目錄結構,讓咱們對Android程序有一個總體全面的認識。android

image

(Lesson2_HelloWorld 顯示效果)程序員

image

(Lesson2_HelloWorld 目錄結構)web

Lesson2_HelloWorld程序的建立過程能夠參見教學視頻:第二講視頻:建立 Lesson2_HelloWorld數據庫

接下來咱們逐個部分加以講解:apache

一、Activity類 MainHelloWorld文件淺析瀏覽器

Activity是Android中的視圖部分,負責界面顯示。網絡

package android.basic.lesson2.helloworld;

import android.app.Activity;
import android.os.Bundle;

public class MainHelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

能夠看到MainHelloWorld是Activity的子類,子類要重寫onCreate方法。setContentView(R.layout.main)方法是給Activity設置能夠顯示的視圖(View),視圖由R類負責尋找。

二、R文件淺析

咱們看到Gen目錄下有個R.Java文件,R文件由ADT自動生成,程序員不須要也不要去修改它,R文件負責調用應用程序中的非代碼資源。

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found.  It
* should not be modified by hand.
*/

package android.basic.lesson2.helloworld;

public final class R {
public static final class attr {
}
public static final class color {
public static final int red=0x7f050000;
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int TextView01=0x7f060000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
public static final int tagline=0x7f040002;
}
}
從R文件中能夠看到每個資源都會有一個整數和它相對應。

三、res/layout/main.xml文件淺析 – 佈局layout

咱們看到有個res目錄,也就是resource目錄,這個目錄下存放資源文件,資源文件的統一管理,也是Android系統的一大特點。如今要注意看的是layout目錄下的main.xml。這個文件的內容是有關用戶界面佈局和設計的。在桌面程序設計領域採用XML也許比較新穎,可是在網頁設計領域,這個就很日常了。同窗們能夠用html來類比xml在佈局中的用途。

<?xml version=」1.0″ encoding=」utf-8″?>
<LinearLayout xmlns:android=」http://schemas.android.com/apk/res/android」
android:orientation=」vertical」
android:layout_width=」fill_parent」
android:layout_height=」fill_parent」
android:gravity=」center」
>
<TextView
android:layout_width=」wrap_content」
android:layout_height=」wrap_content」
android:textColor=」#0f0″
android:textSize=」30px」
android:text=」@string/hello」
/>
<TextView
android:text=」@string/tagline」
android:id=」@+id/TextView01″
android:layout_width=」wrap_content」
android:textSize=」14px」
android:textColor=」@color/red」
android:layout_height=」wrap_content」>
</TextView>
</LinearLayout>

從以上代碼能夠看到整個程序界面由一個線性佈局控件(LinearLayout)和2個文本框控件(TextView)組成。res的其餘目錄裏的其餘文件也都是相關的資源描述。

四、AndroidManifest.xml文件淺析

在每一個應用程序的根目錄都會有一個AndroidManifest.xml文件,該文件向Android操做系統描述了,本程序所包括的組件,所實現的功能,能處理的數據,要請求的資源等等。學過Java Web開發的同窗能夠用Web應用程序裏的web.xml來類比這個AndroidManifest.xml文件。

<?xml version=」1.0″ encoding=」utf-8″?>
<manifest xmlns:android=」http://schemas.android.com/apk/res/android」
package=」android.basic.lesson2.helloworld」
android:versionCode=」1″
android:versionName=」1.0″>
<application android:icon=」@drawable/icon」 android:label=」@string/app_name」>
<activity android:name=」.MainHelloWorld」
android:label=」@string/app_name」>
<intent-filter>
<action android:name=」android.intent.action.MAIN」 />
<category android:name=」android.intent.category.LAUNCHER」 />
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion=」8″ />
</manifest>

咱們看到Manifest是根節點,節點屬性裏有versionCode和versionName來表示應用程序的版本;裏面能夠包含0個或1個application元素,application能夠包含多個activity組件等等,具體的內容咱們在接下來的課程裏詳細講解。

五、Android.jar文件淺析

做爲一個Java項目,一般狀況下都會引入要用到的工具類,也就是Jar包,在Android開發中,絕大部分開發用的工具包都被封裝到一個名叫Android.jar的文件裏了。若是咱們在Eclipse中展開來看,能夠看到j2se中的包,apache項目中的包,還有Android自身的包文件。在這裏咱們簡單瀏覽一下Android的包文件:

android.app :提供高層的程序模型、提供基本的運行環境
android.content :包含各類的對設備上的數據進行訪問和發佈的類
android.database :經過內容提供者瀏覽和操做數據庫
android.graphics :底層的圖形庫,包含畫布,顏色過濾,點,矩形,能夠將他們直接繪製到屏幕上.
android.location :定位和相關服務的類
android.media :提供一些類管理多種音頻、視頻的媒體接口
android.net :提供幫助網絡訪問的類,超過一般的java.net.* 接口
android.os :提供了系統服務、消息傳輸、IPC 機制
android.opengl :提供OpenGL 的工具
android.provider :提供類訪問Android 的內容提供者
android.telephony :提供與撥打電話相關的API 交互
android.view :提供基礎的用戶界面接口框架
android.util :涉及工具性的方法,例如時間日期的操做
android.webkit :默認瀏覽器操做接口
android.widget :包含各類UI 元素(大部分是可見的)在應用程序的屏幕中使用

本節課程就到這裏。

相關文章
相關標籤/搜索