I have been trying to get my BackupAgent to backup my app SQLite database for a couple days now, and I'm having no luck.
From my understanding, it would need to backup the database.db file... and then restore upon installation... But at installation there isn't a database created yet, so I'm not sure how this would work. I've tried to create the databases folder onRestore() and still nothing.
My preferences are restored... (but only from the very first time they backed up even though I have manually triggered the backup) -> adb shell bmgr run
I have been reading all of the posts in regards to this but none seem to work 100%.
I tried to copy the database.db file into the "files" folder, back that up, and then restore it... it kind of worked once, except the database.db file was 0kb once restored into the files folder.
Do I need to somehow create the database on first boot of app and then restore?
I'm stuck.
Suggestions?
Here is my manifest:
<application
android:allowBackup="true"
android:backupAgent=".utilities.BackupAgent"
android:restoreAnyVersion="true">
...
<meta-data
android:name="com.google.android.backup.api_key"
android:value="..."/>
</application>
Here is my BackupAgent:
public final String DATABASE_BACKUP_KEY = "database";
private File databaseDir = new File(Environment.getDataDirectory() + "/data/" + PACKAGE_NAME + "/databases");
@Override
public void onCreate()
{
log.info("onCreate()");
super.onCreate();
// A Helper for our Preferences, this name is the same name we use when saving SharedPreferences
SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PreferencesInfo.PREFERENCES);
addHelper(PreferencesInfo.HELPER_KEY, helper);
SharedPreferencesBackupHelper customHelper = new SharedPreferencesBackupHelper(this, PreferencesInfo.CUSTOM_PREFERENCES);
addHelper(PreferencesInfo.CUSTOM_HELPER_KEY, customHelper);
FileBackupHelper dbs = new FileBackupHelper(this, DBHelper.DB_NAME);
addHelper(DATABASE_BACKUP_KEY, dbs);
}
@Override
public File getFilesDir()
{
File path = getDatabasePath(DBHelper.DB_NAME);
return path.getParentFile();
}
@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) throws IOException
{
log.info("onBackup(...)");
synchronized (DBHelper.dbLock)
{
log.info("onBackup in-lock");
super.onBackup(oldState, data, newState);
}
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException
{
log.info("onRestore called");
synchronized (DBHelper.dbLock)
{
log.info("onRestore in-lock");
super.onRestore(data, appVersionCode, newState);
}
}
Aucun commentaire:
Enregistrer un commentaire