Jus*_*ips 14 sqlite android upgrade
首先,我做了一个搜索,找不到我的问题的具体答案,所以这里...
我正在编写我的第一个Android应用程序,并计划拥有Lite版本(有限功能)和付费版本(完整功能).
Lite和Pro版本将使用相同的SQLite数据库结构,如果用户以Lite版本启动并升级到Pro版本,我不希望它们丢失他们在Lite版本中创建的数据.
由于Lite和Pro版本(根据我的理解)必须在单独的包中以允许Android Market区分它们,Pro版本如何看到Lite数据库?
非常感谢你的答案.
And*_*ite 12
我做了什么似乎适用于Hexaddicus,是Lite和Pro版本作为同一用户运行,然后在第一次运行Pro版本时,复制Lite数据库.然后通知用户该副本.
android:sharedUserId在两个产品中设置相同...
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.package"
android:sharedUserId="com.mycompany.package" <---- IMPORTANT
android:versionCode="10"
android:versionName="@string/app_version"
android:installLocation="auto">
Run Code Online (Sandbox Code Playgroud)
然后是复制DB的代码......
try {
final Context free_context = this.createPackageContext("com.mycompany.package", Context.CONTEXT_INCLUDE_CODE);
final File full_db = this.getDatabasePath(GameData.DATABASE_NAME);
final File full_db_dir = full_db.getParentFile();
final File free_db = free_context.getDatabasePath(GameData.DATABASE_NAME);
final File free_db_dir = free_db.getParentFile();
if (free_db.exists() == false) return;
if (full_db_dir.exists() == false) full_db_dir.mkdir();
if (full_db.exists() == false) full_db.createNewFile();
FileUtils.copyDirectory(free_db_dir, full_db_dir);
this.gameData.getWritableDatabase();
}
catch (NameNotFoundException e) {
/* do nothing here since this is an semi expected case */
Log.w("mytag", "No Lite version found");
} catch (IOException e) {
Log.w("mytag", "Failed to create file");
}
}
Run Code Online (Sandbox Code Playgroud)
唯一的缺点是,一旦复制完成,两个版本之间就没有同步.您还必须确保Lite版本的用户正在运行正在运行的版本,sharedUserId否则副本将失败.
更新:请考虑ChrisCashwells的评论和答案,因为他提出了一个有效的观点,我不记得我在他的例子中做了什么.
| 归档时间: |
|
| 查看次数: |
1783 次 |
| 最近记录: |