android 的settings 数据管理了系统的普通设置,比如wifi、蓝牙状态,当前本机语言,屏幕亮度等等一些相关的系统属性值。这些数据主要是存储在数据库中,对应的URI为:content://settings/system和content://settings/secure,这两个是主要的,目前也只是涉及到这两个数据库表的使用。
数据库名字是settings.db 创建了两个表 system, secure 要往某个表里写东西,可以直接叫到 Settings.System.put**() 跟上contentresolver。值,同样也可以Settings.System.get** 用法类似。当然也可以Settings.secure.get put , 安全表主要用来记录一些网络配置信息。这个完全可以通过sqliteexpert打开看看。
比如取值:Settings.Secure.getInt(getContentResolver() , Settings.Secure.WIFI_ON);
比如设值:Settings.Secure.putInt(getContentResolver(), Settings.Secure.ADB_ENABLED, 0); //设置0和1等...
第一开机时候,查询时候,会指定数据库名字版本
private static final String DATABASE_NAME = "settings.db";
// Please, please please. If you update the database version, check to make sure the
// database gets upgraded properly. At a minimum, please confirm that 'upgradeVersion'
// is properly propagated through your change. Not doing so will result in a loss of user
// settings.
private static final int DATABASE_VERSION = 80;
然后oncreate时候,创建表,然后加载默认数据。
//如何自定义一个值,进行设置改变:
1:frameworks/base/bore/java/android/provider/Settings.java
/**{@hide}*/ //记得此注释
public static final String ACCESSIBILITY_GESTURE_LIGHT_SENSOR_ENABLED = "accessibility_gesture_light_sensor_enabled";
2:frameworks/base/bore/java/android/SettingProvider/res/values/defaults.xml
定义一个默认值 <bool name="accessibility_gesture_light_sensor_enabled">true</bool>
3:DatabaseHelper.java下面
if (upgradeVersion == 83) {
// 1. Setting whether screen magnification is enabled.
// 2. Setting for screen magnification scale.
// 3. Setting for screen magnification auto update.
db.beginTransaction();
SQLiteStatement stmt = null;
try {
stmt = db.compileStatement("INSERT INTO secure(name,value) VALUES(?,?);");
loadBooleanSetting(stmt,
Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED,
R.bool.def_accessibility_display_magnification_enabled);
stmt.close();
stmt = db.compileStatement("INSERT INTO secure(name,value) VALUES(?,?);");
loadBooleanSetting(stmt,
Settings.Secure.ACCESSIBILITY_GESTURE_LIGHT_SENSOR_ENABLED,
R.bool.accessibility_gesture_light_sensor_enabled);
。。。。。
}
}
此外可以进行上面取值,设值了。