从精简版升级,当Android应用程序到Pro版本保持相同的SQLite数据库精简版、应用程序、版本、数据库

2023-09-04 09:06:18 作者:不成魔不快活

首先,我做了搜索,并不能找到一个具体的回答我的问题所以这里去...

First of all I have done a search and can't find a specific answer to my question so here goes...

我写我的第一个Android应用程序,并计划有一个精简版的版本(有限的功能)和一个付费版(全功能)。

I am writing my first Android application and plan to have a Lite version (limited features) and a paid version (full features).

的精简版和专业版将使用相同​​的SQLite数据库的结构,如果用户开始使用精简版的版本和升级到专业版,我不希望他们失去他们在精简版的版本创建的数据。

The Lite and Pro version will use the same SQLite database structure, and if the user starts with the Lite version and upgrades to the Pro version, I dont want them to lose the data they have created in the Lite version.

由于精简版和Pro版本将(从我的理解)必须分开包装,以便在Android Market来区分它们,怎么能专业版看到精简版数据库?

As the Lite and Pro versions will (from my understanding) have to be in separate packages to allow the Android Market to distinguish between them, how can the Pro version see the Lite database?

提前为您的回答非常感谢。

Many thanks in advance for your answers.

推荐答案

我所做的,似乎工作Hexaddicus,是同时拥有精简版,并运行相同的用户,然后的第一次运行Pro版本专业版,复制过来的精简版数据库。然后通知副本的用户。

What I did in and it seems to work for Hexaddicus, is have both Lite and Pro versions run as the same user and then on the first time run of the Pro version, copy the Lite database over. Then inform the user of the copy.

设置安卓sharedUserId 在这两种产品是相同的...

Set the android:sharedUserId to be the same in both products...

<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">

然后是code复制数据库...

And then the code to copy the 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");
    }
}

这样做的唯一的缺点是,一旦复制完成后,有两个版本之间没有同步。您还必须确保的精简版版本的用户正在运行正在运行一个版本的 sharedUserId 或者复制会失败。

更新:请考虑ChrisCashwells意见,回答过,因为他带来了一个有效的点,我不记得我在他的例子做了的

 
精彩推荐
图片推荐