I have a database from assets. I want to backup and restore my database. BakupAgent:
public class MBackupAgent extends BackupAgentHelper {
public static final String PREFS = "data_prefs";
public static final String PREFS_BACKUP_KEY = "myprefs";
// Allocate a helper and add it to the backup agent
@Override
public void onCreate() {
SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS);
addHelper(PREFS_BACKUP_KEY, helper);
FileBackupHelper hosts = new FileBackupHelper(this,
"../databases/" + DBHelper.DB_NAME);
addHelper(DBHelper.DB_NAME, hosts);
}
@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
ParcelFileDescriptor newState) throws IOException {
Log.d("ConnectBot.BackupAgent", "onBackup called");
synchronized (DBHelper.dbLock) {
Log.d("ConnectBot.BackupAgent", "onBackup called");
super.onBackup(oldState, data, newState);
}
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode,
ParcelFileDescriptor newState) throws IOException {
Log.d("ConnectBot.BackupAgent", "onRestore called");
synchronized (DBHelper.dbLock) {
Log.d("ConnectBot.BackupAgent", "onRestore in-lock");
super.onRestore(data, appVersionCode, newState);
}
}
Calling onRestore works fine. But onBackup doesn't. I can make backup only through adb shell. I call dataChanged like this inside SQLiteOpenHelper:
public void setUserName(String name){
synchronized (dbLock) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name);
db.update(TABLE_NAME, values,"oid="+1,null);
db.close();
}
BackupManager.dataChanged("my package name");
}
What I wasn't taken?
Try follow this: http://developer.android.com/tools/help/bmgr.html By default there is no guarantee when onBackup is called. Trigger backup immediately with "adb shell bmgr run".