在设置(settings)应用中,我们可以把一些值(int,boolean,string)保存在db数据库。这个值,我们可以在不同的app应用中使用。我们可以非常方便的在此上添加一些简单的代码,实现一个新的变量在db数据库中,方便不同的app来共同使用。
/**
* Let user pick default install location.
*
* @hide
*/
public static final String SET_INSTALL_LOCATION = "set_install_location";
MOVED_TO_GLOBAL.add(Settings.Global.SET_INSTALL_LOCATION);
loadSetting(stmt, Settings.Global.SET_INSTALL_LOCATION, 0);
import android.provider.Settings;
boolean userSetInstLocation = (Settings.Global.getInt(getContentResolver(),
Settings.Global.SET_INSTALL_LOCATION, 0) != 0);
public static final String CAR_DOCK_SOUND = "car_dock_sound";
MOVED_TO_GLOBAL.add(Settings.Global.CAR_DOCK_SOUND);
loadStringSetting(stmt, Settings.Global.CAR_DOCK_SOUND,R.string.def_car_dock_sound);
/system/media/audio/ui/Dock.ogg
import android.provider.Settings;
String whichSound = null;
whichSound = Settings.Global.CAR_DOCK_SOUND;
ContentResolver cr = mContext.getContentResolver();
final String soundPath = Settings.Global.getString(cr, whichSound);
(1)在任意APP中使用样例:
int vipFunction = Settings.System.getInt(getContentResolver(), Settings.System.HY_VIP_FUNCTION, -1);
(2)修改的文件:
frameworks\base\packages\settingsprovider\src\com\android\providers\settings\DatabaseHelper.java
private void loadSystemSettings(SQLiteDatabase db) {
SQLiteStatement stmt = null;
try {
stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
+ " VALUES(?,?);");
loadBooleanSetting(stmt, Settings.System.DIM_SCREEN,
R.bool.def_dim_screen);
loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
R.integer.def_screen_off_timeout);
// Set default cdma DTMF type
loadSetting(stmt, Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, 0);
// Set default hearing aid
loadSetting(stmt, Settings.System.HEARING_AID, 0);
// Set default tty mode
loadSetting(stmt, Settings.System.TTY_MODE, 0);
loadIntegerSetting(stmt, Settings.System.SCREEN_BRIGHTNESS,
R.integer.def_screen_brightness);
loadBooleanSetting(stmt, Settings.System.SCREEN_BRIGHTNESS_MODE,
R.bool.def_screen_brightness_automatic_mode);
loadDefaultAnimationSettings(stmt);
loadBooleanSetting(stmt, Settings.System.ACCELEROMETER_ROTATION,
R.bool.def_accelerometer_rotation);
loadDefaultHapticSettings(stmt);
loadBooleanSetting(stmt, Settings.System.NOTIFICATION_LIGHT_PULSE,
R.bool.def_notification_pulse);
loadUISoundEffectsSettings(stmt);
loadIntegerSetting(stmt, Settings.System.POINTER_SPEED,
R.integer.def_pointer_speed);
/** Add by HY [wangxianming] add VIP Function in 2014-02-21 by start @{ */
loadBooleanSetting(stmt, Settings.System.HY_VIP_FUNCTION,
R.bool.hy_def_vip_function_value);
/** Add by HY [wangxianming] add VIP Function in 2014-02-21 by end @} */
frameworks\base\core\java\android\provider\Settings.java
/**
* Name of activity to use for wallpaper on the home screen.
*
* @deprecated Use {@link WallpaperManager} instead.
*/
@Deprecated
public static final String WALLPAPER_ACTIVITY = "wallpaper_activity";
/**
* @deprecated Use {@link android.provider.Settings.Global#AUTO_TIME}
* instead
*/
@Deprecated
public static final String AUTO_TIME = Global.AUTO_TIME;
/**
* @deprecated Use {@link android.provider.Settings.Global#AUTO_TIME_ZONE}
* instead
*/
@Deprecated
public static final String AUTO_TIME_ZONE = Global.AUTO_TIME_ZONE;
/**
* Display times as 12 or 24 hours
* 12
* 24
*/
public static final String TIME_12_24 = "time_12_24";
/**
* Add by HY [wangxianming] add VIP Function in 2014-02-21 by start @{
*
* Values 1: is open this function 0: is close this function
*
* @hide
*/
public static final String HY_VIP_FUNCTION = "hy_vip_function";
/** Add by HY [wangxianming] add VIP Function in 2014-02-21 by end @} */
/**
* Settings to backup. This is here so that it's in the same place as the settings
* keys and easy to update.
*
* NOTE: Settings are backed up and restored in the order they appear
* in this array. If you have one setting depending on another,
* make sure that they are ordered appropriately.
*
* @hide
*/
public static final String[] SETTINGS_TO_BACKUP = {
STAY_ON_WHILE_PLUGGED_IN, // moved to global
WIFI_USE_STATIC_IP,
WIFI_STATIC_IP,
WIFI_STATIC_GATEWAY,
WIFI_STATIC_NETMASK,
WIFI_STATIC_DNS1,
WIFI_STATIC_DNS2,
BLUETOOTH_DISCOVERABILITY,
BLUETOOTH_DISCOVERABILITY_TIMEOUT,
DIM_SCREEN,
SCREEN_OFF_TIMEOUT,
SCREEN_BRIGHTNESS,
SCREEN_BRIGHTNESS_MODE,
SCREEN_AUTO_BRIGHTNESS_ADJ,
VIBRATE_INPUT_DEVICES,
MODE_RINGER, // moved to global
MODE_RINGER_STREAMS_AFFECTED,
MUTE_STREAMS_AFFECTED,
VOLUME_VOICE,
VOLUME_SYSTEM,
VOLUME_RING,
VOLUME_MUSIC,
VOLUME_ALARM,
VOLUME_NOTIFICATION,
VOLUME_BLUETOOTH_SCO,
VOLUME_VOICE + APPEND_FOR_LAST_AUDIBLE,
VOLUME_SYSTEM + APPEND_FOR_LAST_AUDIBLE,
VOLUME_RING + APPEND_FOR_LAST_AUDIBLE,
VOLUME_MUSIC + APPEND_FOR_LAST_AUDIBLE,
VOLUME_ALARM + APPEND_FOR_LAST_AUDIBLE,
VOLUME_NOTIFICATION + APPEND_FOR_LAST_AUDIBLE,
VOLUME_BLUETOOTH_SCO + APPEND_FOR_LAST_AUDIBLE,
TEXT_AUTO_REPLACE,
TEXT_AUTO_CAPS,
TEXT_AUTO_PUNCTUATE,
TEXT_SHOW_PASSWORD,
AUTO_TIME, // moved to global
AUTO_TIME_ZONE, // moved to global
TIME_12_24,
/** Add by HY [wangxianming] add VIP Function in 2014-02-21 by start @{ */
HY_VIP_FUNCTION,
/** Add by HY [wangxianming] add VIP Function in 2014-02-21 by end @} */
DATE_FORMAT,
HY_WALLPAPER_MODE,
DTMF_TONE_WHEN_DIALING,
DTMF_TONE_TYPE_WHEN_DIALING,
HEARING_AID,
TTY_MODE,
SOUND_EFFECTS_ENABLED,
HAPTIC_FEEDBACK_ENABLED,
POWER_SOUNDS_ENABLED, // moved to global
DOCK_SOUNDS_ENABLED, // moved to global
LOCKSCREEN_SOUNDS_ENABLED,
SHOW_WEB_SUGGESTIONS,
NOTIFICATION_LIGHT_PULSE,
SIP_CALL_OPTIONS,
SIP_RECEIVE_CALLS,
POINTER_SPEED,
VIBRATE_WHEN_RINGING
};
frameworks\base\packages\settingsprovider\res\values\Defaults.xml------http://------------设置初始值
false
9
true
-1
false
当年,我看到一位大神在framework下添加此例子时,觉得他们好牛啊,在framework下的settings db数据库中都能添加数据,这要写多少代码啊。今天,正好要解一个bug,当我把framework的代码全部导入到source insight中,看了一下这块的代码,觉得添加一个数据在settings模块的db数据库中这也太简单了。
“绝知此事要躬行”-----这对于程序员来说,确实是一条精辟的古语啊。
许多时候,对于未知的恐惧和对困难的过大估计,让我们失去了去做事情的勇气。但是,当我们克服问题去亲手把这件事情做完后,我们会发现,原来事实的真像简单的如此让人发紫啊。
(1)http://blog.csdn.net/xxxsz/article/details/7480354
SystemProperties与Settings.system