PackageManagerService安装包的机制如下:
在 AdroidManifest.xml 里面定义安装位置可以有四种情况:1. 如果没有定义安装位置,表示安装在手机内存上;
2. android:installLocation = "auto",表示先查看手机内存是否足够,如果够就安装在手机内存上,不够就安装在 T 卡上;
3. android:installLocation = "internalOnly",表示安装在手机内存上;
4. android:installLocation = "preferExternal" ,表示安装在 T 卡上;
在 packages\apps\Settings\src\com\android\settings中ApplicationSettings.java
// 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;
}
});
}
修改Settings.Secure.SET_INSTALL_LOCATION此值即可显示用户选择菜单
可以按照如下方式更改,这样用户就能自己选择安装位置:
打开 frameworks/base/packages/SettingsProvider/DatabaseHelper.java将loadSetting(stmt, Secure.SET_INSTALL_LOCATION, 0);改为 loadSetting(stmt, Secure.SET_INSTALL_LOCATION, 1);
将loadSetting(stmt, Settings.Secure.SET_INSTALL_LOCATION, 0);改为 loadSetting(stmt, Settings.Secure.SET_INSTALL_LOCATION, 1);编译后,就能在 Settings --> Applications 里面能看到安装位置选项.
如何设置安装位置默认为SD卡
将 loadSetting(stmt, Secure.DEFAULT_INSTALL_LOCATION,PackageHelper.APP_INSTALL_AUTO);修改为
loadSetting(stmt, Secure.DEFAULT_INSTALL_LOCATION,PackageHelper.APP_INSTALL_EXTERNAL);
将 loadSetting(stmt, Settings.Secure.DEFAULT_INSTALL_LOCATION,PackageHelper.APP_INSTALL_AUTO);修改为
loadSetting(stmt, Settings.Secure.DEFAULT_INSTALL_LOCATION,PackageHelper.APP_INSTALL_EXTERNAL);