在设置(settings)应用中,我们可以把一些值(int,boolean,string)保存在db数据库。这个值,我们可以在不同的app应用中使用。我们可以非常方便的在此上添加一些简单的代码,实现一个新的变量在db数据库中,方便不同的app来共同使用。
1.关键类和文件
frameworks\base\packages\settingsprovider\src\com\android\providers\settings\databaseHelper.java-------给变量赋值,我们可以把变量的初始值写到下面的defaults.xml文件中,使用后面的修改和统一管理。frameworks\base\packages\settingsprovider\res\values\defaults.xml----------放置变量的初始值
有四种类型(bool, integer, string, fraction)四种类型
<bool name="def_sound_effects_enabled">true</bool>
<integer name="def_max_dhcp_retries">9</integer>
<string name="def_location_providers_allowed" translatable="false">gps</string>
<fraction name="def_window_animation_scale">100%</fraction>
frameworks\base\core\java\android\provider\settings.java----------定义变量
2.我们以SET_INSTALL_LOCATION为例,让我们知道这个变量是如何定义使用的:
(1) frameworks\base\core\java\android\provider\settings.java---定义变量:/**
* 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);
(2) frameworks\base\packages\settingsprovider\src\com\android\providers\settings\ databaseHelper.java------给变量赋值
在 loadGlobalSettings(SQLiteDatabase db)方法中:
loadSetting(stmt, Settings.Global.SET_INSTALL_LOCATION, 0);
(3)如何使用:
import android.provider.Settings;
boolean userSetInstLocation = (Settings.Global.getInt(getContentResolver(),
Settings.Global.SET_INSTALL_LOCATION, 0) != 0);
3.我们再以CAR_DOCK_SOUND为例,让我们看看这个变量是如何定义使用的:
(1) frameworks\base\core\java\android\provider\settings.java---定义变量:public static final String CAR_DOCK_SOUND = "car_dock_sound";
MOVED_TO_GLOBAL.add(Settings.Global.CAR_DOCK_SOUND);
(2) frameworks\base\packages\settingsprovider\src\com\android\providers\settings\ databaseHelper.java------给变量赋值
loadStringSetting(stmt, Settings.Global.CAR_DOCK_SOUND,R.string.def_car_dock_sound);
(3) frameworks\base\packages\settingsprovider\res\values\defaults.xml-----定义初始值
<string name="def_car_dock_sound" translatable="false">/system/media/audio/ui/Dock.ogg</string>
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);
4.在DB数据库中添加一个标志位(HY_VIP_FUNCTION)的样例:
(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://------------设置初始值
<!-- Development settings -->
<bool name="def_stay_on_while_plugged_in">false</bool>
<!-- Number of retries for connecting to DHCP.
Value here is the same as WifiStateMachine.DEFAULT_MAX_DHCP_RETRIES -->
<integer name="def_max_dhcp_retries">9</integer>
<!-- Default for Settings.Secure.USER_SETUP_COMPLETE -->
<bool name="def_user_setup_complete">true</bool>
<integer name="hy_def_wallpaper_mode_value">-1</integer>
<!-- Add by HY [wangxianming] add VIP Function in 2014-02-21 by start @{ -->
<bool name="hy_def_vip_function_value">false</bool>
<!-- Add by HY [wangxianming] add VIP Function in 2014-02-21 by end @} -->
5.写在后面的话:
当年,我看到一位大神在framework下添加此例子时,觉得他们好牛啊,在framework下的settings db数据库中都能添加数据,这要写多少代码啊。今天,正好要解一个bug,当我把framework的代码全部导入到source insight中,看了一下这块的代码,觉得添加一个数据在settings模块的db数据库中这也太简单了。
“绝知此事要躬行”-----这对于程序员来说,确实是一条精辟的古语啊。
许多时候,对于未知的恐惧和对困难的过大估计,让我们失去了去做事情的勇气。但是,当我们克服问题去亲手把这件事情做完后,我们会发现,原来事实的真像简单的如此让人发紫啊。