我通过以下链接http://developer.android.com/training/cloudsync/backupapi.html将应用数据同步到Google服务器。我做了文档中提到的所有步骤。但每次我尝试恢复数据都会崩溃。我写的代码如下所示。
清单文件
<application
android:allowBackup="true"
android:restoreAnyVersion="true"
android:backupAgent="TheBackupAgent"
android:icon="@drawable/icn_launcher"
android:label="@string/app_name" >
<meta-data android:name="com.google.android.backup.api_key"
android:value="AEdP-_NxcBxt_Zq56pCzqg" />
<activity
android:name=".BackUpAvtivity"
android:configChanges="orientation"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Light.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
** TheBackUpAgent Class **
public class TheBackupAgent extends BackupAgentHelper
{
// A key to uniquely identify the set of backup data
static final String FILES_BACKUP_KEY = "myfiles";
BackupManager bm;
// Allocate a helper and add it to the backup agent
@Override
public void onCreate()
{
FileBackupHelper helper = new FileBackupHelper(this, "HistoryDates", "EndDates", "Settings",
"Reminders", "LoveDates", "Weights", "Temperatures", "Notes");
addHelper(FILES_BACKUP_KEY, helper);
}
public void requestBackup()
{
bm = new BackupManager(this);
bm.dataChanged();
}
public void restoreData()
{
bm = new BackupManager(this);
bm.requestRestore(
new RestoreObserver()
{
public void restoreStarting(int error)
{
System.out.println(error);
}
}
);
}
}
BackUpAvtivity Class
在这个课程中,我有两个按钮用于备份,一个用于恢复。每个按钮内的代码都是。
buttonBackup.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
TheBackupAgent bckupAgent = new TheBackupAgent();
bckupAgent.requestBackup();
}
buttonRestore.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
TheBackupAgent bckupAgent = new TheBackupAgent();
bckupAgent.restoreData();
}
崩溃报告
11-27 11:08:58.463: E/AndroidRuntime(16281): FATAL EXCEPTION: main
11-27 11:08:58.463: E/AndroidRuntime(16281): java.lang.IllegalArgumentException: Package not found
11-27 11:08:58.463: E/AndroidRuntime(16281): at android.os.Parcel.readException(Parcel.java:1429)
11-27 11:08:58.463: E/AndroidRuntime(16281): at android.os.Parcel.readException(Parcel.java:1379)
11-27 11:08:58.463: E/AndroidRuntime(16281): at android.app.backup.IBackupManager$Stub$Proxy.beginRestoreSession(IBackupManager.java:852)
11-27 11:08:58.463: E/AndroidRuntime(16281): at android.app.backup.BackupManager.requestRestore(BackupManager.java:146)
11-27 11:08:58.463: E/AndroidRuntime(16281): at in.plackal.lovecyclesfree.TheBackupAgent.restoreData(TheBackupAgent.java:33)
11-27 11:08:58.463: E/AndroidRuntime(16281): at in.plackal.lovecyclesfree.BackupActivity$1$4.onClick(BackupActivity.java:338)
11-27 11:08:58.463: E/AndroidRuntime(16281): at android.view.View.performClick(View.java:4261)
11-27 11:08:58.463: E/AndroidRuntime(16281): at android.view.View$PerformClick.run(View.java:17356)
11-27 11:08:58.463: E/AndroidRuntime(16281): at android.os.Handler.handleCallback(Handler.java:615)
11-27 11:08:58.463: E/AndroidRuntime(16281): at android.os.Handler.dispatchMessage(Handler.java:92)
11-27 11:08:58.463: E/AndroidRuntime(16281): at android.os.Looper.loop(Looper.java:137)
11-27 11:08:58.463: E/AndroidRuntime(16281): at android.app.ActivityThread.main(ActivityThread.java:4921)
11-27 11:08:58.463: E/AndroidRuntime(16281): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 11:08:58.463: E/AndroidRuntime(16281): at java.lang.reflect.Method.invoke(Method.java:511)
11-27 11:08:58.463: E/AndroidRuntime(16281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-27 11:08:58.463: E/AndroidRuntime(16281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-27 11:08:58.463: E/AndroidRuntime(16281): at dalvik.system.NativeStart.main(Native Method)