[Android5.1]系统默认设置
Posted 迷途小书童Eric
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Android5.1]系统默认设置相关的知识,希望对你有一定的参考价值。
settings.db
Andorid系统中设置的相关信息存放在数据库中,具体位置为:
/data/data/com.android.providers.settings/database/settings.db
该数据库就是轻量级的关系型数据库SQLite。Android对该数据库的操作封装成了许多SQLiteXXX类。framework层或应用层使用这些类就可以实现从数据库的读写操作。
那在Android系统中怎样查看settings.db中的内容呢?
其实很简单:
- adb shell进到系统中
- 运行以下命令,打开数据库
$ sqlite3 /data/data/com.android.providers.settings/database/settings.db
sqlite3>
- 使用sqlite的shell指令,查看数据库内容,内容如下:
sqlite3>.tables //列出数据库中存在的表
bluetooth_devices bookmarks secure system globalsqlite3>select * from secure; //列出表格secure中的所有内容
1|location_providers_allowed|
2|mock_location|0
3|backup_enabled|0
4|backup_transport|android/com.android.internal.backup.LocalTransport
5|mount_play_not_snd|1
6|mount_ums_autostart|0
7|mount_ums_prompt|1
8|mount_ums_notify_enabled|1
9|accessibility_script_injection|0
……
19|screensaver_components|com.android.deskclock/com.android.deskclock.Screensaver
20|screensaver_default_component|com.android.deskclock/com.android.deskclock.Screensaver
21|accessibility_display_magnification_enabled|0
22|accessibility_display_magnification_scale|2.0
23|accessibility_display_magnification_auto_update|1
25|immersive_mode_confirmations|
26|install_non_market_apps|0
27|wake_gesture_enabled|1
28|lock_screen_show_notifications|1
29|lock_screen_allow_private_notifications|1
30|sleep_timeout|-1
33|android_id|26b7afacf1a071ad
……
47|enabled_input_methods|com.android.inputmethod.latin/.LatinIME
49|input_methods_subtype_history|com.sohu.inputmethod.sogou/.SogouIME;-1
50|default_input_method|com.android.inputmethod.latin/.LatinIME
defaults.xml
Anroid系统有一些默认设置,这些默认设置就存放在defaults.xml中,路径为:
frameworks/base/packages/SettingsProvider/res/values/defaults.xml
内容如下:
<resources>
<bool name="def_dim_screen">true</bool>
<integer name="def_screen_off_timeout">60000</integer>
<integer name="def_button_light_off_timeout">1500</integer>
<integer name="def_sleep_timeout">-1</integer>
<bool name="def_airplane_mode_on">false</bool>
<integer name="def_container_permission_disabled">0</integer>
<bool name="def_theater_mode_on">false</bool>
<!-- Comma-separated list of bluetooth, wifi, and cell. -->
<string name="def_airplane_mode_radios" translatable="false">cell,bluetooth,wifi,nfc,wimax</string>
<string name="airplane_mode_toggleable_radios" translatable="false">bluetooth,wifi,nfc</string>
...
...
<string name="config_default_input_method" translatable="false">com.sohu.inputmethod.sogou/.SogouIME</string>
<string name="config_enabled_input_method" translatable="false">com.sohu.inputmethod.sogou/.SogouIME</string>
</resources>
load settings
在手机第一次开机或恢复出厂设置时,系统会创建settings.db数据库及各个表格,然后从defaults.xml中读出默认设置并存入数据库相应的表格中。代码如下:
public class DatabaseHelper extends SQLiteOpenHelper
...
//创建secure表格
private void createSecureTable(SQLiteDatabase db)
db.execSQL("CREATE TABLE secure (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name TEXT UNIQUE ON CONFLICT REPLACE," +
"value TEXT" +
");");
db.execSQL("CREATE INDEX secureIndex1 ON secure (name);");
//创建global表格
private void createGlobalTable(SQLiteDatabase db)
db.execSQL("CREATE TABLE global (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name TEXT UNIQUE ON CONFLICT REPLACE," +
"value TEXT" +
");");
db.execSQL("CREATE INDEX globalIndex1 ON global (name);");
@Override
public void onCreate(SQLiteDatabase db)
//创建system表格
db.execSQL("CREATE TABLE system (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name TEXT UNIQUE ON CONFLICT REPLACE," +
"value TEXT" +
");");
db.execSQL("CREATE INDEX systemIndex1 ON system (name);");
createSecureTable(db);
// 只有系统为单用户时,才创建global表格
// Only create the global table for the singleton 'owner' user
if (mUserHandle == UserHandle.USER_OWNER)
createGlobalTable(db);
//创建 bluetooth_devices表格
db.execSQL("CREATE TABLE bluetooth_devices (" +
"_id INTEGER PRIMARY KEY," +
"name TEXT," +
"addr TEXT," +
"channel INTEGER," +
"type INTEGER" +
");");
//创建bookmarks表格
db.execSQL("CREATE TABLE bookmarks (" +
"_id INTEGER PRIMARY KEY," +
"title TEXT," +
"folder TEXT," +
"intent TEXT," +
"shortcut INTEGER," +
"ordering INTEGER" +
");");
db.execSQL("CREATE INDEX bookmarksIndex1 ON bookmarks (folder);");
db.execSQL("CREATE INDEX bookmarksIndex2 ON bookmarks (shortcut);");
// Populate bookmarks table with initial bookmarks
boolean onlyCore = false;
try
onlyCore = IPackageManager.Stub.asInterface(ServiceManager.getService(
"package")).isOnlyCoreApps();
catch (RemoteException e)
if (!onlyCore)
loadBookmarks(db);
// Load initial volume levels into DB
loadVolumeLevels(db);
// Load inital settings values
loadSettings(db); //加载默认设置
...
代码比较简单,创建各种各种表格,初始化各表格中的内容。我们重点看一下loadSettings():
private void loadSettings(SQLiteDatabase db)
loadSystemSettings(db);
loadSecureSettings(db);
// The global table only exists for the 'owner' user
if (mUserHandle == UserHandle.USER_OWNER)
loadGlobalSettings(db);
可见,先后初始化了system、secure和global三个表格。这三个函数的具体实现差不多,我们以loadSystemSettings(),看一下具体做了什么:
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 touch light timeout.
loadIntegerSetting(stmt, Settings.System.BUTTON_LIGHT_OFF_TIMEOUT,
R.integer.def_button_light_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);
...
loadUISoundEffectsSettings(stmt);
loadIntegerSetting(stmt, Settings.System.POINTER_SPEED,
R.integer.def_pointer_speed);
finally
if (stmt != null) stmt.close();
代码也是很简单,调用sqlite语句:
INSERT OR IGNORE INTO system(name,value) VALUES(xxx,xxx);
往system表格中插入各数据项。
这里使用了“INSERT OR IGNORE INTO“,意思是,如果数据库没有该数据项,就插入新的数据;如果有,就跳过这条数据。
下面,挑其中一句代码说明:
public static final String SCREEN_OFF_TIMEOUT = "screen_off_timeout";
loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
R.integer.def_screen_off_timeout);
R.integer.def_screen_off_timeout就是前面讲的defaults.xml中的默认设置
<integer name="def_screen_off_timeout">60000</integer>
这样,在settings.db的system表格中会存在以下表项:
12|screen_off_timeout|60000
以上是关于[Android5.1]系统默认设置的主要内容,如果未能解决你的问题,请参考以下文章