0

I'm trying to use this method to copy the database contents from my free app to my paid app but I'm quite lost at the last step:

Check for the presence of the provider & load the data into your paid app.

I can check for the existence of the provider like so:

    List<ProviderInfo> providers = getPackageManager().queryContentProviders(
    "com.mypackagename.content.MyProvider",android.os.Process.myUid(), 0);

but I have no idea how to do the copying?

I tried getting a ContentResolver like this:

    ContentResolver resolver = getContentResolver();
    ContentProviderClient client = resolver.acquireContentProviderClient(Uri.parse("content://com.mypackagename"));

I haven't been able to test this yet, but even if it works I'm lost as to the next step. How am I meant to get the data out of one database into the other?

Community
  • 1
  • 1
DavidBriggs
  • 400
  • 3
  • 15

1 Answers1

1

So I solved this. It wasn't too tricky at all.

I was on the right track with the ContentProviderClient code.

ContentResolver resolver = getContentResolver();
ContentProviderClient client = resolver.acquireContentProviderClient(Uri.parse("content://com.mypackagename.free"));

Then you need the URIs to the table that you want to copy from, I believe these must be hardcoded.

Uri CONTENT_URI_TABLE1 = Uri.parse("content://com.mypackagename.free/table1");

Then you need to query the client

Cursor table1Cursor = client.query(CONTENT_URI_TABLE1, null, null, null, null);

Now you need to have a function which can create an object from a cursor and you iterate through the cursor creating items and then adding them to your database. You probably already have this to create items after querying the table.

table1Cursor.moveToFirst();
FancyItem tempItem = providerUtils.createFancyItemFromCursor(table1Cursor);

Then you add this temporary item to the new database using whatever function you were using to do that previously.

    providerUtils.addFancyItem(tempItem);

Iterate through the cursor and that's it.

DavidBriggs
  • 400
  • 3
  • 15