当LaunchProvider.DatabaseHelper.convertDatabase(SQLiteDatabase db)在Android默认启动器中返回true时?
我在阅读安卓2.2默认启动器的源代码时遇到问题。当LaunchProvider.DatabaseHelper.convertDatabase(SQLiteDatabase db)在Android默认启动器中返回true时?,android,launcher,Android,Launcher,我在阅读安卓2.2默认启动器的源代码时遇到问题。 LaunchProvider.DatabaseHelper的源代码段: @Override public void onCreate(SQLiteDatabase db) { if (LOGD) Log.d(TAG, "creating new launcher database"); db.execSQL("CREATE TABLE favorites (" + "_id INTEGER PRIMARY KEY,"
LaunchProvider.DatabaseHelper的源代码段:
@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 db)中有查询代码。我很困惑。为什么它在onCreate()中查询?convertDatabase(SQLiteDatabase db)何时返回true?我的Android版本是2.2可以从日志消息中检索某些上下文:
if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
如果存在旧格式的数据库,convertDatabase()
使用copyFromCursor
将所有条目复制到新条目,其中cursor指向旧实例,然后将其删除。因此,如果方法已找到并转换了旧格式的数据库实例,则该方法将返回true
,否则返回false
正如启动器实际调用的
Launcher2
一样,它很可能试图找到前辈的踪迹。谢谢您的回答。这是否意味着旧启动器与Launcher2具有相同的权限和不同的数据库文件名?