7.0以后,不再使用原来的DatabaseHelper来处理数据库,现在都用新的SettingsProvider
framework/base/packages/SettingsProvider
/**
* Legacy settings database helper class for {@link SettingsProvider}.
*
* IMPORTANT: Do not add any more upgrade steps here as the global,
* secure, and system settings are no longer stored in a database
* but are kept in memory and persisted to XML.
*
* See: SettingsProvider.UpgradeController#onUpgradeLocked
*
* @deprecated The implementation is frozen. Do not add any new code to this class! 看这
*/
@Deprecated
class DatabaseHelper extends SQLiteOpenHelper {}
1.如何添加一个新的字段
用insertSettingLocked来添加
/**
* You must perform all necessary mutations to bring the settings
* for this user from the old to the new version. When you add a new
* upgrade step you *must* update SETTINGS_VERSION.
*
* This is an example of moving a setting from secure to global.
*
* // v119: Example settings changes.
* if (currentVersion == 118) {
* if (userId == UserHandle.USER_OWNER) {
* // Remove from the secure settings.
* SettingsState secureSettings = getSecureSettingsLocked(userId);
* String name = "example_setting_to_move";
* String value = secureSettings.getSetting(name);
* secureSettings.deleteSetting(name);
*
* // Add to the global settings.
* SettingsState globalSettings = getGlobalSettingsLocked();
* globalSettings.insertSetting(name, value, SettingsState.SYSTEM_PACKAGE_NAME);
* }
*
* // Update the current version.
* currentVersion = 119;
* }
*/
private int onUpgradeLocked(int userId, int oldVersion, int newVersion) {
if (DEBUG) {
Slog.w(LOG_TAG, "Upgrading settings for user: " + userId + " from version: "
+ oldVersion + " to version: " + newVersion);
}
int currentVersion = oldVersion;
if (currentVersion == 146) {
final SettingsState globalSettings = getGlobalSettingsLocked();
globalSettings.insertSettingLocked(Settings.Global.SHAKE_FLASHLIGHT_ON, "0",null
, true,
SettingsState.SYSTEM_PACKAGE_NAME);
currentVersion = 147;
}
}
注意: SETTINGS_VERSION 一定要更新,比如现在是currentVersion = 147,那你就要把SETTINGS_VERSION改成147