对于我的Android项目,我尝试遵循并实施Android开发者数据备份指南(http://developer.android.com/guide/topics/data/backup.html),但数据备份和还原不起作用。有人可以帮忙举例吗?
答案 0 :(得分:2)
经过进一步调查,我找到了实施Android数据备份和恢复的步骤。他们是:
以下是具有备份支持的Manifest xml文件示例:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.project"> <application android:allowBackup="true" android:backupAgent="md5f576d3976d691fac04b078d1718cab3.MyBackupAgent"> <meta-data android:name="com.google.android.backup.api_key" >android:value="{Your unique Registration Key}" /> </application>
这是MyBackupAgent类的Java SharedPreferenceBackupHelper示例:
import android.app.backup.BackupAgentHelper;
import android.app.backup.SharedPreferencesBackupHelper;
import android.util.Log;
public class MyBackupAgent extends BackupAgentHelper{
static final String PREFS = "myprefs";
static final String PREFS_BACKUP_KEY = "myprefs";
@Override
public void onCreate() {
Log.i("MyFileBackupAgent", "****** Enter BACKUP CLASS *******");
SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS);
addHelper(PREFS_BACKUP_KEY, helper);
Log.i("MyFileBackupAgent", "****** Exit BACKUP CLASS ********");
}
}
这是MyBackupAgent类的C#Xamarin FileBackupHelper示例:
公共类MyBackupAgent:BackupAgentHelper
{
string myProtectData =&#34; File.bin&#34 ;;
string myPersistentData =&#34; Data.bin&#34 ;;
string myDBase =&#34; Database.db&#34 ;;
public override void OnCreate()
{
Console.WriteLine ("****** Enter Backup Files Helpers *********");
base.OnCreate ();
try
{
FileBackupHelper dbkh = new FileBackupHelper (this, myProtectData);
this.AddHelper ("PROTECT_backup", dbkh);
FileBackupHelper persisth = new FileBackupHelper (this, myPersistentData);
this.AddHelper ("PERSIST_backup", persisth);
FileBackupHelper dbh = new FileBackupHelper (this, myDBase);
this.AddHelper ("DATABASE_backup", dbh);
Console.WriteLine ("********* All 3 files backuped *********");
}
catch {
Console.WriteLine ("******* Backup AddHelper Exception ERROR *********");
}
Console.WriteLine ("******** Exit Backup Files Helpers ********");
}
public override void OnBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)
{
Console.WriteLine ("****** Override OnBackup called ******");
base.OnBackup(oldState, data, newState);
}
public override void OnRestore (BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
{
Console.WriteLine ("****** Override OnRestore called ******");
base.OnRestore(data, appVersionCode, newState);
}
}
出于测试目的,可以使用Android SDK命令提示符工具执行即时备份。试试这些命令:
确保启用数据备份:adb shell bmgr enable true
要请求数据备份:adb shell bmgr备份your.package.name
要启动数据备份:adb shell bmgr运行
要卸载您的应用:adb uninstall your.package.name
然后安装您的应用程序:adb install your.package.name
您的手机设备备份设置怎么样?确保已建立WiFi连接。此外,在设备设置下,确保&#34;备份我的数据&#34;和&#34;自动恢复&#34;已经过检查,您已输入有效的备份帐户电子邮件ID。
最后,要跟踪您的备份上传时间戳,请使用www.google.com/settings/dashboard(这是您的个人Google帐户,与您的Android手机设备备份设置中的Google帐户相匹配)