Android應用程序的默認安裝位置以及是否可移動取決於開發者在其AndroidManifest.xml中的設置:
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:installLocation="auto"
android:versionName="1.0">
android:installLocation的值有三個 internalOnly ,auto,preferExternal,缺省值為internalOnly
internalOnly表示該應用程序只能安裝到手機內部存儲中。
auto表示由系統決定該應用程序安裝到手機內部存儲中還是SD卡中。 如果有SD卡且應用程序大於5M的話,安裝到SD卡中,否則安排到手機內部存儲中
preferExternal表示如果有SD卡就把該應用程序只能安裝到SD卡中,否則安裝到手機內部存儲中。
android:installLocation為internalOnly時,用戶在"Setting"->"Application"->"Manage applications"中不能把應用程序在SD卡與內存中相互移動
android:installLocation為auto或preferExternal時,用戶在"Setting"->"Application"->"Manage applications"中可以把應用程序在SD卡與內存中相互移動
另外,adb shell 中可以使用 pm setInstallLocation 2 命令中強行更改安裝位置。2代表的是強制安裝在SD卡中,0代表自動,1代表強制裝到手機內部存儲中。
在代碼中,對於高於Android 2.2的手機中,可以通過ApplicationInfo.FLAG_EXTERNAL_STORAGE 標記可以判斷應用是否安裝在Sdcard上,對於低於Android 2.2的手機可以通過ApplicationInfo的sourceDir屬性為/sdcard/開頭來確定APK安裝的位置。
示例1
PackageManager pm=ctx.getPackageManager();
ApplicationInfo appInfo=pm.getApplicationInfo(pkgName,0);
if((appInfo.flags &ApplicationInfo.FLAG_EXTERNAL_STORAGE)!=0){
// App on sdcard
System.out.println(pkgName+" install on sdcard");