Android 的基本概念

Android

簡介

使用方法

開發工具

開始寫程式

視覺化介面

視覺元件

對話元件

核心物件

事件處理

資料儲存

查詢功能

影音功能

繪圖功能

網路功能

衛星地圖

特殊功能

資源管理

裝置管理

系統核心

問題與回答

刷機升級

常用軟體

Eclipse

教學錄影

訊息

相關網站

參考文獻

最新修改

簡體版

English

Android 應用通常用 Java 語言撰寫,然後透過 aapt 工具轉換包裝成單一的 *.apk 檔案,一個 *.apk 檔就是一個完整的 Android 的應用程式,可以下載到 Android 手機中執行。

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 vehicle 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 應用都有一個獨立的環境: (1) 屬於獨立的行程 (Process)、 (2) 擁有獨立的虛擬機 (VM)、(3) 擁有獨立的 Linux ID,專有的權限設定等。

In many ways, each Android application lives in its own world:

  • By default, every application runs in its own Linux process. Android starts the process when any of the application's code needs to be executed, and shuts down the process when it's no longer needed and system resources are required by other applications.
  • Each process has its own virtual machine (VM), so application code runs in isolation from the code of all other applications.
  • By default, each application is assigned a unique Linux user ID. Permissions are set so that the application's files are visible only to that user and only to the application itself — although there are ways to export them to other applications as well.

Android 有一個重要的設計理念,每個程式都可以用其他程式的任何一個畫面,因此如果其他程式已經有相簿功能,你就可以直接使用該程式的相簿,而不需要重寫。

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 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 of the other application when the need arises.

為了達成這個要求,Android 的程式必須每個畫面都可以隨時被啟動,而沒有一定的先後順序關係,也沒有像 main 這樣的程式啟動點,只有元件 (Component 的概念),每個 Component 都可以隨時被系統啟動或停止。因此 Android 設計了下列四種型態的元件,分別是 Activity、Service、Broadcast Receivers、Content Providers。

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 entry 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

Activity 與 Intent

Android 的設計相當的玄妙難以理解,如果您第一次看到 Intent 與 Activity 這些名詞,勢必會感到相當程度的困惑。但是當您使用過品質優良的 Android 手機 (像是 HTC Legend) 之後,您將會開始讚嘆其運作的流暢性,然後反思 Android 的設計哲學。

通常 Activity 代表一個畫面,而 Intent 則代表一個跨越畫面的轉換動作,於是整個 Android 系統操作就相當於下列流程。

畫面 = (流轉) => 畫面 = (流轉) => 畫面 = (流轉) => …=> 畫面 = (流轉) => …
Activity = (Intent) => Activity = (Intent) => … => Activity = (Intent) => …

這個設計就是為了達成 Android 的核心理念,每個程式都可以用其他程式的任何一個畫面所設計的。

在上述流程中,我們將 Activity 類比為手機畫面,而 Intent 則類比為畫面轉換的動作,稱為流轉。

在目前的畫面中,如果使用者操作了某些動作,像是選取了某個連結後,就會開始造成流轉,然後進入另一個畫面中。

但是 Google 為了讓流轉這個動作能夠更有彈性,所以在 Android、Intent 與 AndroidManifest.xml 上進行了巧妙的設計,設計方法如下。

參考文獻

  1. Application Fundamentals — http://developer.android.com/guide/topics/fundamentals.html
  2. Android學習筆記(4)-學習Intent的使用 — http://forum.hkgphone.com/viewthread.php?tid=52

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License