Keeping the same SQLite database when upgrading and Android app from Lite to Pro version

前端 未结 3 756
说谎
说谎 2021-01-31 06:06

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

I am writing my first Android application and plan to have a Lite vers

3条回答
  •  被撕碎了的回忆
    2021-01-31 06:48

    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.

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

    
    

    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");
        }
    }
    

    The only downside of this is that once the copy is done, there is no syncing between the two versions. You also have to make sure that the user of the Lite version is running a version that is running as the sharedUserId or the copy will fail.

    Update: please consider ChrisCashwells comments and answer too since he brings up a valid point and I can't remember what I did in his example.

提交回复
热议问题