我在閱讀Android 2.2默認啓動器的源代碼時遇到問題。
LauncherProvider.DatabaseHelper的源代碼段:當LauncherProvider.DatabaseHelper.convertDatabase(SQLiteDatabase db)在Android默認啓動器中返回true時?
@Override
public void onCreate(SQLiteDatabase db) {
if (LOGD)
Log.d(TAG, "creating new launcher database");
db.execSQL("CREATE TABLE favorites (" + "_id INTEGER PRIMARY KEY,"
+ "title TEXT," + "intent TEXT," + "container INTEGER,"
+ "screen INTEGER," + "cellX INTEGER," + "cellY INTEGER,"
+ "spanX INTEGER," + "spanY INTEGER," + "itemType INTEGER,"
+ "appWidgetId INTEGER NOT NULL DEFAULT -1,"
+ "isShortcut INTEGER," + "iconType INTEGER,"
+ "iconPackage TEXT," + "iconResource TEXT," + "icon BLOB,"
+ "uri TEXT," + "displayMode INTEGER" + ");");
// Database was just created, so wipe any previous widgets
if (mAppWidgetHost != null) {
mAppWidgetHost.deleteHost();
sendAppWidgetResetNotify();
}
if (!convertDatabase(db)) {
// Populate favorites table with initial favorites
loadFavorites(db);
}
}
private boolean convertDatabase(SQLiteDatabase db) {
if (LOGD)
Log.d(TAG,
"converting database from an older format, but not onUpgrade");
boolean converted = false;
final Uri uri = Uri.parse("content://" + Settings.AUTHORITY
+ "/old_favorites?notify=true");
final ContentResolver resolver = mContext.getContentResolver();
Cursor cursor = null;
try {
cursor = resolver.query(uri, null, null, null, null);
} catch (Exception e) {
// Ignore
}
// We already have a favorites database in the old provider
if (cursor != null && cursor.getCount() > 0) {
try {
converted = copyFromCursor(db, cursor) > 0;
} finally {
cursor.close();
}
if (converted) {
resolver.delete(uri, null, null);
}
}
if (converted) {
// Convert widgets from this import into widgets
if (LOGD)
Log.d(TAG, "converted and now triggering widget upgrade");
convertWidgets(db);
}
return converted;
}
onCreate()
被調用來創建數據庫當在系統中沒有數據庫。在onCreate()的尾部,它調用convertDatabase(SQLiteDatabase db)。並且在convertDatabase(SQLiteDatabase數據庫)中有查詢代碼。我很困惑。爲什麼在onCreate()中查詢?當convertDatabase(SQLiteDatabase數據庫)將返回true?我的Android版本是2.2
感謝您的回答。這是否意味着舊啓動程序與Launcher2具有相同的權限和不同的數據庫文件名? – Xiao 2012-07-06 08:31:55