我有一个资产数据库。我想备份和恢复我的数据库。 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);
}
}
调用onRestore可以正常工作。但是onBackup并没有。我只能通过adb shell进行备份。我在SQLiteOpenHelper中调用dataChanged:
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");
}
我没有采取什么?
答案 0 :(得分:0)
请尝试按照以下步骤操作:http://developer.android.com/tools/help/bmgr.html 默认情况下,无法保证调用onBackup。使用" adb shell bmgr run"。
立即触发备份