標籤:android installlocation
/***************************************************
* TODO: description .
* @author: gao_chun
* @since: 2015-4-1
* @version: 1.0.0
* @remark: 轉載請註明出處
**************************************************/
在Froyo(android 2.2,API Level:8)中引入了android:installLocation
通過設定該屬性可以使得開發人員以及使用者決定程式的安裝位置.
android:installLocation屬於AndroidManifest.XML中的manifest節點.如下所示:
[html] view plaincopy
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="string"
- android:sharedUserId="string"
- android:sharedUserLabel="string resource"
- android:versionCode="integer"
- android:versionName="string"
- android:installLocation=["auto" | "internalOnly" | "preferExternal"] >
- . . .
- </manifest>
官方API:
android:installLocation
The default install location for the application.
The following keyword strings are accepted:
| Value |
Description |
"internalOnly" |
The application must be installed on the internal device storage only. If this is set, the application will never be installed on the external storage. If the internal storage is full, then the system will not install the application. This is also the default behavior if you do not define android:installLocation. |
"auto" |
The application may be installed on the external storage, but the system will install the application on the internal storage by default. If the internal storage is full, then the system will install it on the external storage. Once installed, the user can move the application to either internal or external storage through the system settings. |
"preferExternal" |
The application prefers to be installed on the external storage (SD card). There is no guarantee that the system will honor this request. The application might be installed on internal storage if the external media is unavailable or full, or if the application uses the forward-locking mechanism (not supported on external storage). Once installed, the user can move the application to either internal or external storage through the system settings. |
android:installLocation可以設定為"auto"、"internalOnly"、"preferExternal"三個值中的任何一個.
auto:程式可能被安裝在外部儲存介質上(例如:SD Card),但是預設會被安裝到手機記憶體中.當手機記憶體為空白時,程式將被安裝到外部儲存介質上.當程式安裝到手機上後,使用者可以決定把程式放在外部儲介質還是記憶體中.
internalOnly:預設值.當設定為該值時,程式只能被安裝在記憶體中,如果記憶體為空白,則程式將不能成功安裝.
preferExternal:將程式安裝在外部儲存介質上,但是系統不保證程式一定會被安裝到外部儲存介質上.當外部儲存介質不可以或空時,程式將被安裝到記憶體中.程式使用了forward-locking機制時也將被安裝到記憶體中,因為外部儲存不支援此機制.程式安裝後,用戶可以自由切換程式應該在外部還是內部儲存介質上.
注意:當程式使用了Google Play的Copy Protection特性時,只能安裝到記憶體中.
當程式被安裝到外部儲存介質時,
① .apk檔案將被移動到外部儲存介質上,但是程式的資料仍然會在記憶體中
② 儲存.apk檔案的容器將會使用一個隨機產生的密鑰進行加密,這樣只有安裝該程式的設定可以使用存在外部儲存介質上的資料.
警告:當外部儲存介質被卸載時,安裝在該外部儲存介質上的程式將立刻被終止掉!
向後相容性:
聲明了android:installLocation,但android:minSdkVersion小於8時,我們使用不低於Froyo的AVD進行編譯,這樣在低於Froyo的系統中android:installLocation將被忽略,而不低於Froyo的系統中將使用我們指定的android:installLocation.
建議:
當程式具有如下行為時我們不應該將程式安裝到外部儲存介質上
①Service
正在啟動並執行服務將被終止,當外部儲存介質被重新載入時服務不會被重啟.
②Alarm Service
鬧鐘服務將被取消,開發人員必須在外部儲存介質重新載入後重新註冊鬧鐘服務.
③Input Method Engines
IME將被換成系統IME,當外部儲存介質被重新載入後使用者可以通過系統設定來啟動我們的IME
④Live Wallpapers
我們的動態壁紙將被替換為預設的動態壁紙.外部儲存介質重載後,使用者可以更換回來.
⑤Live Folders
我們的動態資料夾將被移出.
⑥App Widgets
我們的小組件將被移出,通常只有系統重啟後我們的小組件才可用.
⑦Account Managers
使用AccountManager建立的賬戶將會消失,直至儲存介質被重新載入.
⑧Sync Adapters
只有外部儲存介質被重新載入時,我們的同步功能才可用
⑨Device Administrators
我們的DeviceAdminReceiver將會失效
⑩監聽開機結束事件
系統會在載入外部儲存介質之前發送ACTION_BOOT_COMPLETED廣播.因此安裝在外部儲存介質的程式將不能接受開機廣播.
通常,只要我們沒有使用上述的特性,我們就可以將程式安裝到外部儲存介質上.例如,大的遊戲程式.當APK檔案很大時我們應該認真的考慮是否要將程式移動到外部儲存介質上以協助使用者節省記憶體.
簡析android:installLocation