- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在阅读Android 2.2默认启动器的源代码时遇到了一个问题。
LauncherProvider.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?我的安卓版本是2.2
最佳答案
可以从日志消息中检索一些上下文:
if (LOGD) Log.d(TAG, "converting database from an older format, but not onUpgrade");
如果有旧格式的数据库,convertDatabase()
使用 copyFromCursor
将所有条目复制到新格式,其中光标指向旧实例,然后将其删除。因此,如果该方法找到并转换了旧格式的数据库实例,该方法将返回 true
,否则返回 false
。
因为启动器实际上调用了 Launcher2
,所以它很可能试图找到前任的踪迹。
关于android - 当 LauncherProvider.DatabaseHelper.convertDatabase(SQLiteDatabase db) 在 Android 默认启动器中返回 true 时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9374673/
我正在尝试访问LauncherProvider。你可以找到它的源代码here 我尝试像这样查询这个ContentProvider: Uri uri = new Uri.Builder().scheme
我在阅读Android 2.2默认启动器的源代码时遇到了一个问题。 LauncherProvider.DatabaseHelper源码段: @Override public void onCreate
我是一名优秀的程序员,十分优秀!