我的环境是基于msm8x12的android4.3。
在做android系统时,每个公司对其都有其自己的定制,比如默认用什么输入法,用什么语言,是否开启ADB调试等,这些当你第一次开机的时候,都会获取这些默认值然后去设置。那么这些放在哪呢,如何去操作呢?
msm8x12\frameworks\base\core\java\android\provider下方的就是数据库操作封装,Android系统提供大量的SettingsProvider,除本处说的Settings,常用的还有管理联系人(ContactsContract)、多媒体数据(MediaStore)等。
对于如何使用,就是在你封装的SettingsProvider中使用Settings.System.putXXX();Settings.System.getXXX()来进行设置和查询。
对于我们此处要说的Settings,它的默认值在frameworks/base/packages/SettingsProvider/res/values/defaults.xml
这些默认值只是一些整数值或者字符串(第一次开机要用到的值用于初始化数据库,等数据库形成了,这些值就没用了)。
如何将其存到数据库呢,在frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
就是对defaults.xml所有初始值的进行初始化数据库,每一个值都存于一个对应的属性中。
对于要设置的属性定义在哪?
在frameworks/base/packages/SettingsProvider/src/com/android/providers/Settings.java定义,都是字符串常量。这样数据库中就形成了“属性--值”这样的映射存储。
在DatabaseHelper.java中的onCreate加载音量默认值,系统设置默认值,大概如下,
public void onCreate(SQLiteDatabase db) {
...
// Load initial volume levels into DB
loadVolumeLevels(db); //电话音量,铃声音量,系统音量默认值初始化到数据库
// Load inital settings values
loadSettings(db); //系统相关默认值初始化到数据库
}
private void loadSettings(SQLiteDatabase db) {
loadSystemSettings(db); //基本系统设置默认值加载
loadEnhancedSystemSettings(db); //系统增强功能默认值加载
loadSecureSettings(db); //有关系统安全的默认值加载
// The global table only exists for the 'owner' user
if (mUserHandle == UserHandle.USER_OWNER) {//面向拥有者的全局系统设置默认值加载
loadGlobalSettings(db);
loadEnhancedGlobalSettings(db);
}
}
设置中的默认值除了在defaults.xml中,还有customize.xml,只要在设置中设置完重启后能保存状态的基本都是数据库存储。
customize.xml值的意义:
1)def_custom_sys_volume,bool
是否启用自定义各种音频源,如果为false就会用默认的值,定义在frameworks\base\media\java\android\media\AudioManager.java
/** @hide Default volume index values for audio streams */
public static final int[] DEFAULT_STREAM_VOLUME = new int[] {
4, // STREAM_VOICE_CALL
7, // STREAM_SYSTEM
5, // STREAM_RING
11, // STREAM_MUSIC
6, // STREAM_ALARM
5, // STREAM_NOTIFICATION
7, // STREAM_BLUETOOTH_SCO
7, // STREAM_SYSTEM_ENFORCED
11, // STREAM_DTMF
11, // STREAM_TTS
4 // STREAM_INCALL_MUSIC
};
如果为true,那么就会用defaults.xml接下来的def_ringtone_volume,def_music_volume等值进行初始化数据库
2)def_time_format,string
时间格式,取值:12或者24
3)def_date_format,string
日期格式,取值:mm/dd/yyyy,dd/mm/yyyy,yyyy/mm/dd
4)def_input_method,string
默认使用的输入法, Default Input Method, its value is from inputmethod's package name and main class name such as "com.android.inputmethod.pinyin/.PinyinIME"
5)def_enable_input_methods,string
使能的输入法,可以有多个,用冒号:分割。一般如下如何来定制系统默认使用的输入法
loadStringSetting(stmt, Settings.Secure.ENABLED_INPUT_METHODS, R.string.def_enable_input_methods);
loadStringSetting(stmt, Settings.Secure.DEFAULT_INPUT_METHOD, R.string.def_input_method);
6)def_enable_data_roaming,bool
数据漫游开关
7)def_enable_mobile_data,bool
移动数据开关关
例子:
以设置默认输入法为例,
a)在上述defaults.xml中添加新默认值
当然如果已经存在某个name了,那么直接改值即可,上述值面对不同输入法是不同,比如默认英文输入法就改为值com.android.inputmethod.latin/.LatinIME,默认百度输入法该值为com.baidu.input/.ImeService,你安装完输入法可以在设备的shell中使用ime list -s命令来看到该值。
b)在上述DatabaseHelper.java中的loadSecureSettings()函数中添加(其他loadXXX中添加当然也可以)
loadStringSetting(stmt, Settings.Secure.ENABLED_INPUT_METHODS, R.string.def_input_method);
loadStringSetting(stmt, Settings.Secure.DEFAULT_INPUT_METHOD, R.string.def_input_method);
这样建立起数据库,属性为ENABLED_INPUT_METHODS设置完后,这样在安卓settings中,对应的输入法是打钩的,设置多个就打钩多个;属性为DEFAULT_INPUT_METHOD设置完后,其对应的输入法就是为默认的输入法了。当然你也可以在DatabaseHelper.java中直接去设置,而不再defaults.xml中放默认值,最好不要这样做。
其它属性:
不管下面是BOOL(0或者1)还是INT最终都是转为string存储的。
全名为Settings.System.XXXX,以下皆省略除XXXX以外的字符
1)DIM_SCREEN
BOOL; @Deprecated; Whether or not to dim the screen; 0=no 1=yes
2)SCREEN_OFF_TIMEOUT
INT; The timeout in milliseconds before the screen turns off
3)SCREEN_BRIGHTNESS
INT; The screen backlight brightness between 0 and 255
4)SCREEN_BRIGHTNESS_MODE
BOOL; Control whether to enable automatic brightness mode; 0=manual,1=auto
5)ACCELEROMETER_ROTATION
BOOL; Control whether the accelerometer will be used to change screen orientation; 0= 默认禁用旋转,除非应用明确使用,1=默认自动旋转,除非应用明确禁用
6)HAPTIC_FEEDBACK_ENABLED
BOOL; Whether the haptic feedback (long presses, ...) are enabled[触觉反馈]; 0=disable,1=enable
7)NOTIFICATION_LIGHT_PULSE
BOOL; Whether the notification LED should repeatedly flash when a notification is pending
8)DTMF_TONE_WHEN_DIALING
BOOL; Whether the audible DTMF tones are played by the dialer when dialing
9)SOUND_EFFECTS_ENABLED
BOOL; Whether the sounds effects (key clicks, lid open ...) are enabled
10)LOCKSCREEN_SOUNDS_ENABLED
INT; Whether to play sounds when the keyguard is shown and dismissed.
全名为Settings.Secure.XXXX,以下皆省略XXXX以外的字符
这部分数据库的访问为Settings.secure.put/getXXX
1)LOCATION_PROVIDERS_ALLOWED
String; Comma-separated list of location providers that activities may access; "network"仅基站定位,"gps"仅GPS定位,"network,gps"基站、GPS都开放, ""关闭定位
2)ADB_ENABLED
// Don't do this. The SystemServer will initialize ADB_ENABLED from a
// persistent system property instead.
//loadSetting(stmt, Settings.Secure.ADB_ENABLED, 0);
在我的android4.3系统中,使用persist.sys.usb.config属性中增加"adb"值即默认打开adb,不再使用ADB_ENABLE属性直接初始化数据库了。
3)LONG_PRESS_TIMEOUT
INT; The timout for considering a press to be a long press in milliseconds.
4)LOCKSCREEN_DISABLED
BOOL; Whether the lockscreen should be completely disabled
if (SystemProperties.getBoolean("ro.lockscreen.disable.default", false) == true) {
loadSetting(stmt, Settings.System.LOCKSCREEN_DISABLED, "1");
} else {
loadBooleanSetting(stmt, Settings.System.LOCKSCREEN_DISABLED,
R.bool.def_lockscreen_disabled);
}
ro.lockscreen.disable.default属性优先于默认值的。
5)ENABLED_INPUT_METHODS
String; List of input methods that are currently enabled,This is a string containing the IDs of all enabled input methods, each ID separated by ':'
6)DEFAULT_INPUT_METHOD
String; Setting to record the input method used by default, holding the ID of the desired method.
全名为Settings.Global.XXXX,以下皆省略XXXX以外的字符
1)AIRPLANE_MODE_ON
BOOL; Whether Airplane Mode is on.
2)AIRPLANE_MODE_RADIOS
String; A comma separated list of radios that need to be disabled when airplane mode is on. This overrides WIFI_ON and BLUETOOTH_ON, if Wi-Fi and bluetooth are included in the comma separated list;
例如defaults.xml中有
cell,bluetooth,wifi,nfc,wimax
那么当飞行模式开启时,蜂窝网络,蓝牙,wifi,NFC,wimax都得关闭。
3)AIRPLANE_MODE_TOGGLEABLE_RADIOS
String; A comma separated list of radios that should to be disabled when airplane mode is on, but can be manually reenabled by the user
例如defaults.xml中有
bluetooth,wifi,nfc
那么当飞行模式开启时,bluetooth,wifi,nfc会被关闭,但是可以由用户在settings app重新打开。
4)ASSISTED_GPS_ENABLED
BOOL; Whether assisted GPS should be enabled or not [AGPS是否打开]
5)AUTO_TIME
BOOL; Value to specify if the user prefers the date, time and time zone to be automatically fetched from the network (NITZ). 1=yes, 0=no
6)AUTO_TIME_ZONE
BOOL; Value to specify if the user prefers the time zone to be automatically fetched from the network (NITZ). 1=yes, 0=no
7)STAY_ON_WHILE_PLUGGED_IN
INT; Whether we keep the device on while the device is plugged in(eg:charger ofBATTERY_PLUGGED_AC,BATTERY_PLUGGED_USB,BATTERY_PLUGGED_WIRELESS)
8)WIFI_SLEEP_POLICY
INT; The policy for deciding when Wi-Fi should go to sleep (which will in turn switch to using the mobile data as an Internet connection);0 == never, 1 == only when plugged in, 2 == always
9)MODE_RINGER
INT; Ringer mode. This is used internally, changing this value will not change the ringer mode. See AudioManager
/**
* Ringer mode that will be silent and will not vibrate. (This overrides the
* vibrate setting.)
*
* @see #setRingerMode(int)
* @see #getRingerMode()
*/
public static final int RINGER_MODE_SILENT = 0;
/**
* Ringer mode that will be silent and will vibrate. (This will cause the
* phone ringer to always vibrate, but the notification vibrate to only
* vibrate if set.)
*
* @see #setRingerMode(int)
* @see #getRingerMode()
*/
public static final int RINGER_MODE_VIBRATE = 1;
/**
* Ringer mode that may be audible and may vibrate. It will be audible if
* the volume before changing out of this mode was audible. It will vibrate
* if the vibrate setting is on.
*
* @see #setRingerMode(int)
* @see #getRingerMode()
*/
public static final int RINGER_MODE_NORMAL = 2;
10)WIFI_ON
BOOL; Whether the Wi-Fi should be on. Only the Wi-Fi service should touch this
11)BLUETOOTH_ON
BOOL; Whether bluetooth is enabled/disabled
12)MOBILE_DATA/DATA_ROAMING
BOOL; [数据连接/漫游]
13)INSTALL_NON_MARKET_APPS
BOOL; Whether the package installer should allow installation of apps downloaded from sources other than Google Play,0=only google play,1=other source
14)USB_MASS_STORAGE_ENABLED
BOOL; USB Mass Storage Enabled
15)LOCK_SOUND/UNLOCK_SOUND
String; URI for the "device locked" (keyguard shown) sound/URI for the "device unlocked" sound