探索Settings.System.putInt()
阿新 • • 發佈:2019-02-14
在android系統移植時,經常要儲存系統某一變數的值,最簡單的方法就是儲存到系統資料庫中,而不是儲存在apk的xml中,只要一句話:
讀也非常簡單:
只要帶一個Context,就可以讀寫這個資料庫,讓我感到困惑的是:對資料庫進行操作,插入和修改是兩種不同命令,android是怎麼知道我到底是進行插入還是修改操作呢?於是跟蹤到原始碼裡看個清楚:
首先,資料庫儲存在哪?
原始碼路徑:
\frameworks\base\packages\SettingsProvider\src\com\android\providers\settings
在DatabaseHelper類裡,建立資料庫和表
public class DatabaseHelper extends SQLiteOpenHelper
{
......
privatevoid 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);" );
}
privatevoid 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
publicvoid onCreate(SQLiteDatabase
db) {
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);
// Only create the global table for the singleton 'owner' user
if (mUserHandle == UserHandle.USER_OWNER)
{
createGlobalTable(db);
}
db.execSQL( "CREATE TABLE bluetooth_devices (" +
"_id INTEGER PRIMARY KEY," +
"name TEXT," +
"addr TEXT," +
"channel INTEGER," +
"type INTEGER" +
");" );
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);
}
這裡建立了幾張表,這個資料庫生成的db檔案,儲存成(在android系統):
/data/data/com.android.providers.settings/databases/settings.db