Android特性:
- Application framework enabling reuse and replacement of components
- Dalvik virtual machine optimized for mobile devices
- Integrated browser based on the open source WebKit engine
- Optimized graphics powered by a custom 2D graphics library; 3D graphics based on the OpenGL ES 1.0 specification (hardware acceleration optional)
- SQLite for structured data storage
- Media support for common audio, video, and still image formats (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF)
- GSM Telephony (hardware dependent)
- Bluetooth, EDGE, 3G, and WiFi (hardware dependent)
- Camera, GPS, compass, and accelerometer (hardware dependent)
- Rich development environment including a device emulator, tools for debugging, memory and performance profiling, and a plugin for the Eclipse IDE
架構:
應用程式組件:
- Activity
- Services
- Content Providers
- Broadcast receiver
Manifest 檔案
整個程式的功能清單,包括包的定義、版本聲明,應用程式Activity、ICON、service、intent filter等的聲明。
在Eclipse中可以通過Manifest Editor方便編輯(右鍵開啟)。
<?xml version="1.0" encoding="utf-8"?>
<manifest . . . >
<application . . . >
<activity android:name="com.example.project.FreneticActivity"
android:icon="@drawable/small_pic.png"
android:label="@string/freneticLabel"
. . . >
</activity>
. . .
</application>
</manifest>
Activities and Tasks
一個task就是使用者感覺上的"應用程式",task 是一組相互關聯的activity集合組成的棧(這些activity可能屬於多個應用程式). 棧底就是應用程式啟動時顯示的第一個activity。棧頂activity就是使用者在螢幕上看到的activity。當一個activity啟動一個新的activity時,新的activity就會被壓入棧中。當使用者點擊"back " 按鈕時,位於棧頂的activity將會被彈出棧。我們舉個例子,假設task中有兩個activity,分別是 A 和 B,其中A是位於棧頂的activity,使用者點擊"back"後, A就被彈出棧, B 就成為了新的棧頂activity,然後 activity B 會顯示在螢幕上。
通常來講,task棧中的activity不會被改變排列順序,只有出棧和入棧操作。
A task is a stack of activities, not a class or an element in the manifest file. So there's no way to set values for a task independently of its activities. Values for the task as a whole are set in the root activity. For example, the next section will talk about the "affinity of a task"; that value is read from the affinity set for the task's root activity.
一個task中的所有activity是作為一個整體來移動的--移到前台後者後台。就個例子,假設當前 task 棧中有ABCD4個activity,A是棧頂activity,也就是在螢幕上顯示的activity,使用者點擊了"home"鍵,啟動了一個新的應用程式,則 ABCD 4個activity都會被移動到後台。過了一會,使用者又點擊了"home"鍵,並重新選擇了先前的程式,則 ABCD 都被移動到前台,A 仍然作為棧頂activity 被顯示在螢幕上,然後使用者點擊了"back",A被彈出棧,B作為新的棧頂activity被顯示在螢幕上。
前面所講的都是task的預設行為,我們可以通過某些方法改變task 的預設行為。
不同的task裡的activity是由affinity來區別的.預設情況下每個task裡面的activities都有相同的affinity。但是可以通過<activity> 標籤裡taskAffinity屬性來修改某個activity的affinity,此時需要設定FLAG_ACTIVITY_NEW_TASK標誌,並且allowTaskReparenting 屬性設定為true。關於這點的文檔解釋是:As described earlier, a new activity is, by default, launched into the task of the activity that called startActivity()
. It's pushed onto the same stack as the caller. However, if the Intent object passed to startActivity()
contains the FLAG_ACTIVITY_NEW_TASK
flag, the system looks for a different task to house the new activity. Often, as the name of the flag implies, it's a new task. However, it doesn't have to be. If there's already an existing task with the same affinity as the new activity, the activity is launched into that task. If not, it begins a new task.
注意:在使用FLAG_ACTIVITY_NEW_TASK屬性(singleTask和singleInstance類似)時,無法回退到調用者。
activity在task中載入方式主要有四種:
1.standard(the default mode):一個intent發過來之後,都會產生一個新的activity來響應這個intent;
2.singleTop:保持stack頂端的那個activity.也就是說,如果一個intent是由stack頂端的activity處理的話,將不會建立一個新的activity來處理,而是由原來的那個activity處理;
3.singleTask:對於這個activity則會建立一個task.也就是說,對於這個activity,會分配一個新的affinity;
4.singleInstance:identical to "singleTask
",並保證這個activity唯一.
既然對初始載入的過程有所規定,那麼也就很自然地會有結束過程的一些方式:
acitivity在退出的時候,必然會影響到task的stack,因此,android對acitivity的行為方式也做了規定.
1.alwaysRetainTaskState屬性.保證stack不變.預設狀態下,一個task長期不活動的話,會退化到root activity,也就是彈出stack,只保留底部的一個acitivity.
2.clearTaskOnLaunch屬性.這個會在acitivity載入的時候清空task的stack.
3.finishOnTaskLaunch屬性.某種程度上說這個屬性保證了某項task是唯一的.因為當一個同類的task載入的時候,如果此屬性為true,那麼就會先退出之前的task,然後再載入這個task.
如果FLAG_ACTIVITY_CLEAR_TOP屬性為真,則結束activity時,所有處於該activity堆棧之上的activity都會同時清除,FLAG_ACTIVITY_CLEAR_TOP is most often used in conjunction with FLAG_ACTIVITY_NEW_TASK. When used together, these flags are a way of locating an existing activity in another task and putting it in a position where it can respond to the intent.