android應用程序安裝位置分析

轉自:http://blog.sina.com.cn/s/blog_8984d3f301011pe4.html

一、需求描述

針對應用程序安裝,各產品存在如下疑問:

1. 同一個手機,爲什麼恢復出廠設置後,在Settings->application中沒有preferred Install Location的選項,但是在運行某一個apk(如packageInstaller)之後,在Settings->application中出現了preferred Install Location?

2. 不同apk,同一手機,爲什麼有的apk安裝在SD卡上,有的apk安裝在手機內?

3. 同一個apk,爲什麼有的產品安裝在SD卡上,有的產品存在手機內?

4. 同一個apk,同一個手機,爲什麼有的時候安裝在SD卡上,有的時候安裝在手機內?

答案:以上現象在Froyo和Gingerbread上都是正常的,我們目前是follow的google原始設計,沒有做過修改。原因詳見代碼分析。

二、分析

1、Settings中的安裝位置

通過查看Froyo、Gingerbread版本的文件applicationSettings.java。發現在代碼中定義了安裝位置的設置功能,但是通過開關默認是關閉的(即默認不顯示安裝位置的設置菜單),可以通過設置Settings.Secure.SET_INSTALL_LOCATION 爲1打開,打開後用戶就可以改變應用程序安裝的優選位置了。

在hw_default.xml中配置

settings.system.set_install_location

代碼片段如下:

// Is app default install location set?

boolean userSetInstLocation = (Settings.System.getInt(getContentResolver(),

Settings.Secure.SET_INSTALL_LOCATION, 0) != 0);

if (!userSetInstLocation) {

getPreferenceScreen().removePreference(mInstallLocation);

else {

mInstallLocation.setValue(getAppInstallLocation());

mInstallLocation.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

public boolean onPreferenceChange(Preference preference, Object newValue) {

String value = (String) newValue;

handleUpdateAppInstallLocation(value);

return false;

}

});

}

如圖爲默認用戶可設置的配置:

public static final String DEFAULT_INSTALL_LOCATION = "default_install_location";

在hw_default.xml中配置

Settings.secure.default_install_location

應用程序安裝位置分析應用程序安裝位置分析



2、不同apk,同一手機,爲什麼有的apk安裝在SD卡上,有的apk安裝在手機內

在Android 2.2中新的特性可以支持類似APP2SD卡上,我們的APK文件可以安裝在SD卡上供用戶使用

1. 首先讓你的程序支持SD卡上安裝必須具備設置API Level至少爲8,即androidmanifest.xml的中android:minSdkVersion至少爲8這樣你的APK最終運行時兼容的固件只有2.2了,同時在androidmanifest.xml文件的根節點中必須加入android:installLocation這個屬性,類似代碼如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

android:installLocation="preferExternal"

... >

2. android:installLocation的值主要有preferExternal、auto和internalOnly這三個選項,通常我們設置爲preferExternal可以優先推薦應用安裝到SD卡上,當然最終用戶可以選擇爲內部的ROM存儲上,如果外部存儲已滿,Android內部也會安裝到內部存儲上,auto將會根據存儲空間自適應,當然還有一些應用可能會有特殊的目的,他們一般必須安裝在內部存儲才能可靠運行,設置爲internalOnly比較合適,主要體現在:

Services 服務

Your running Service will be killed and will not be restarted when external storage is remounted. You can, however, register for the ACTION_EXTERNAL_APPLICATIONS_AVAILABLE broadcast Intent, which will notify your application when applications installed on external storage have become available to the system again. At which time, you can restart your Service. Android123提示大家一般實時後臺監控類的應用都應該裝到內部存儲,比較可靠。

Alarm Services 鬧鈴提醒服務

Your alarms registered with AlarmManager will be cancelled. You must manually re-register any alarms when external storage is remounted.

Input Method Engines 輸入法引擎

Your IME will be replaced by the default IME. When external storage is remounted, the user can open system settings to enable your IME again.

Live Wallpapers 活動壁紙

Your running Live Wallpaper will be replaced by the default Live Wallpaper. When external storage is remounted, the user can select your Live Wallpaper again.

Live Folders 活動文件夾

Your Live Folder will be removed from the home screen. When external storage is remounted, the user can add your Live Folder to the home screen again.

App Widgets Widget

Your App Widget will be removed from the home screen. When external storage is remounted, your App Widget will not be available for the user to select until the system resets the home application (usually not until a system reboot).

Account Managers 賬戶管理

Your accounts created with AccountManager will disappear until external storage is remounted.

Sync Adapters 同步適配器

Your AbstractThreadedSyncAdapter and all its sync functionality will not work until external storage is remounted.

Device Administrators 設備管理器

Your DeviceAdminReceiver and all its admin capabilities will be disabled, which can have unforeseeable consequences for the device functionality, which may persist after external storage is remounted.

那麼哪些應用適合安裝在SD卡中呢? Android開發網建議一些佔用資源比較大的遊戲,比如大於3MB的單個文件,不需要長期駐留內存的應用,不具備提醒和實時監控的應用一般放到SD卡上比較合適,不過目前想讓你的應用裝到SD卡上,必須設置API Level至少爲8以上,同時顯示註明android:installLocation。在Android 2.2中新的特性可以支持類似APP2SD卡上,我們的APK文件可以安裝在SD卡上供用戶使用

3、同一個apk,爲什麼有的產品安裝在SD卡上,有的產品存在手機內

這往往是因爲apk中設置爲auto,

有的產品默認在首選設置爲“可卸載的SD卡”,當SD卡空間足夠時,就會安裝在SD卡上,如果SD卡空間不足時,會安裝在手機內。

有的產品默認設爲“設備內部存儲”;只能裝到設備內部了。

4、同一個apk,同一個手機,爲什麼有的時候安裝在SD卡上,有的時候安裝在手機內

這往往是因爲apk中設置爲preferExternal、auto,那麼在首選設置爲“可卸載的SD卡”或“由系統決定”時,SD卡空間足夠時,就會安裝在SD卡上,如果SD卡空間不足時,會安裝在手機內。

現在又有個疑問了,如果apk設置和系統設置有衝突時如何取捨呢?

此處需要後續調試驗證。

從Android系統2.2開始,允許應用程序安裝在sdcard上。

應用程序APK

每個Apk的應用程序可以獨立的設置該應用程序的安裝目錄

在應用程序的androidmanifest.xml文件的根節點中必須加入android:installLocation這個屬性,類似代碼如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android "
android:installLocation ="preferExternal"
... >

android:installLocation 的值主要有preferExternal、auto 和internalOnly這三個選項,通常我們設置爲preferExternal可以優先推薦應用安裝到SD卡上,當然最終用戶可以選擇爲內部的 ROM存儲上,如果外部存儲已滿,Android內部也會安裝到內部存儲上,auto將會根據存儲空間自適應,當然還有一些應用可能會有特殊的目的,他們一般必須安裝在內部存儲才能可靠運行,設置爲internalOnly比較合適,

在S8600和U8600上,我們在Setting --->Application

增加了一個首選安裝位置的選項,在這裏可以選擇設備內部存儲、可卸載sd卡,由系統決定等三個選項。。

現在發現有些apk在安裝的時候,Setting --->Application-->首選安裝位置, 選擇的是優先安裝在可卸載sd卡上,可是卻安裝在

系統中了,並且這些安裝在系統中的應用,可以移動到sdacard上。

經過測試的結果如下:

應用程序APK

系統的Application Settings

安裝後結果

installLocation參數

可卸載sd卡

可卸載sd卡,可以移動

installLocation參數

設備內部存儲

設備內部存儲,不可以移動

installLocation參數

由系統決定

設備內部存儲,不可以移動

auto

可卸載sd卡

設備內部存儲,可以移動

auto

設備內部存儲

設備內部存儲,可以移動

auto

由系統決定

設備內部存儲,可以移動

internalOnly

設備內部存儲

設備內部存儲,不可以移動

internalOnly

可卸載sd卡

設備內部存儲,不可以移動

preferExternal

可卸載sd卡/設備內部存儲

可卸載sd卡,可以移動

目前經過測試發現,在apk中如果聲明爲Auto,Setting --->Application-->首選安裝位置, 選擇可卸載sd卡

這樣的應用程序最後被安裝在 設備內部存儲 中了,並且可以移動到sdcard上。

目前大多數應用都聲明瞭auto,這樣會造成很多應用程序在安裝的時候無法安裝到sdcard,而是直接安裝到了系統中,只能通過移動至sdcard

才能把這些apk安裝在sdcard上,我們手機的系統空間本來就不夠,這樣Auto的也無法直接安裝到sdcard上。

四、結論

通過代碼的分析,發現我們沒有修改google原始設計,以上現象都是正常的。

      文章轉自:http://blog.sina.com.cn/s/blog_8984d3f301011pe4.html

 

發佈了13 篇原創文章 · 獲贊 4 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章