我已經實施了BackupAgent,如Data Backup所述,註冊了API密鑰並在我的Manifest中聲明瞭BackupAgent。 我認爲備份部分的工作很好,當我在命令行中運行adb shell bmgr run
,以下輸出出現在logcat中:在另一方面BackupAgent:「無法恢復包...」
01-11 22:23:09.002: DEBUG/PerformBackupThread(97): starting agent for backup of BackupRequest{app=ApplicationInfo{4547c5b8 com.meins.nightclock} full=false}
01-11 22:23:09.002: DEBUG/BackupManagerService(97): awaiting agent for ApplicationInfo{4547c5b8 com.meins.nightclock}
01-11 22:23:09.013: DEBUG/BackupManagerService(97): agentConnected pkg=com.meins.nightclock [email protected]
01-11 22:23:09.032: DEBUG/BackupHelperDispatcher(9122): handling existing helper 'alarms' [email protected]
01-11 22:23:09.032: DEBUG/BackupHelperDispatcher(9122): handling existing helper 'prefs' [email protected]
01-11 22:23:09.032: VERBOSE/LocalTransport(97): performBackup() pkg=com.meins.nightclock
01-11 22:23:09.032: VERBOSE/LocalTransport(97): Got change set key=alarms:alarms size=16 key64=YWxhcm1zOmFsYXJtcw==
01-11 22:23:09.042: VERBOSE/LocalTransport(97): data size 16
01-11 22:23:09.062: VERBOSE/LocalTransport(97): Got change set key=prefs:com.meins.nightclock_preferences size=265 key64=cHJlZnM6Y29tLm1laW5zLm5pZ2h0Y2xvY2tfcHJlZmVyZW5jZXM=
01-11 22:23:09.072: VERBOSE/LocalTransport(97): data size 265
01-11 22:23:09.072: VERBOSE/LocalTransport(97): finishBackup()
還原部分沒有在所有的工作,對onReceive()
方法不叫。當我重新安裝我的應用程序,它可以(?)是指BackupAgent唯一的輸出是
01-11 22:14:01.042: DEBUG/vending(7426): [100] LocalAssetCache.updateOnePackage(): No local info for com.meins.nightclock
當我運行adb shell bmgr restore com.meins.nightclock
它只是說Unable to restore package com.meins.nightclock
。
我使用以下實現的BackupAgentHelper
package com.meins.nightclock;
import java.io.IOException;
import android.app.backup.BackupAgentHelper;
import android.app.backup.BackupDataInput;
import android.app.backup.BackupDataOutput;
import android.app.backup.FileBackupHelper;
import android.app.backup.SharedPreferencesBackupHelper;
import android.os.ParcelFileDescriptor;
import android.util.Log;
public class MyBackupAgent extends BackupAgentHelper {
public static final Object[] DATA_LOCK = new Object[0];
private static final String PREFS_BACKUP_KEY = "prefs";
private static final String ALARMS_BACKUP_KEY = "alarms";
private DataLayerAlarms d;
@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
ParcelFileDescriptor newState) throws IOException {
if (d.backup() != 0)
return;
synchronized (DATA_LOCK) {
super.onBackup(oldState, data, newState);
}
}
@Override
public void onCreate() {
SharedPreferencesBackupHelper preferencesHelper = new SharedPreferencesBackupHelper(this,
getPackageName() + "_preferences");
addHelper(PREFS_BACKUP_KEY, preferencesHelper);
FileBackupHelper fileHelper = new FileBackupHelper(this, DataLayerAlarms.BACKUP_FILE);
addHelper(ALARMS_BACKUP_KEY, fileHelper);
d = new DataLayerAlarms(this);
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
throws IOException {
Log.d(toString(), "onRestore()");
synchronized (DATA_LOCK) {
super.onRestore(data, appVersionCode, newState);
}
d.restore();
}
}
爲了完整起見,相關清單部分:
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:debuggable="true"
android:backupAgent="MyBackupAgent">
<meta-data
android:name="com.google.android.backup.api_key"
android:value="AEdPqrEAAAAI4MsUOC-[...]" />
...
</application>
有誰知道,爲什麼BackupManager是無法恢復這個包?
我得到同樣的事情。我已經看到它的工作,但突然它不再工作。 – slott 2013-08-23 08:51:05
我也是。它正在工作,並突然停止工作。像wtf .. – coolcool1994 2014-12-14 09:57:14