我在进行Android项目时,尝试遵循并实现了Android开发人员的数据备份指南(http://developer.android.com/guide/topics/data/backup.html),但数据备份和恢复无法正常工作。有没有人能提供一些示例帮助解决问题?
我在进行Android项目时,尝试遵循并实现了Android开发人员的数据备份指南(http://developer.android.com/guide/topics/data/backup.html),但数据备份和恢复无法正常工作。有没有人能提供一些示例帮助解决问题?
android:allowBackup="true"
(这将启用Android的数据备份服务)meta-data android:name="com.google.android.backup.api_key"
android:value=”{你的Android备份服务的唯一注册密钥}”
(你必须使用备份服务注册你的应用程序包名称。要生成密钥,请转到http://developer.android.com/google/backup/signup.html)android:backupAgent=”.MyBackupAgent”
(这是实现你声明的备份代理类的类名)。注意:名字的第一个字符是一个点,目的是为了缩写来定位你的“com.example.project.MyBackupAgent”。如果发生运行时异常(这可能取决于你的项目结构),例如:java.lang.ClassNotFoundException: 没有找到类“com.example.project.MyBackupAgent”,那么建议你反编译你的apk(上传你的apk包到http://www.decompileandroid.com/),并搜索你的MyBackupAgent的绝对路径,并将此路径插入到android:backupAgent=”{absolute path}.MyBackupAgent”中。
<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>
英译中:
这是一个Java SharedPreferenceBackupHelper示例,适用于MyBackupAgent类:
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 ********");
}
}
public class MyBackupAgent: BackupAgentHelper
{
string myProtectData = "File.bin";
string myPersistentData = "Data.bin";
string myDBase = "Database.db";
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);
}
}