我试图让BackupAgent / BackupAgentHelper在6.0以下的Android版本上运行两天。我的目标是通过com.google.android.backup / .BackupTransportService或android / com.android.internal.backup.LocalTransport备份/data/data/com.package/文件夹中的所有文件。
是否存在关于BackupAgentHelper的默认实现的教程,该实现存储多个文件?我已尝试将FileBackupHelper与我在互联网上找到的每个片段一起使用,但这些文件无法恢复。
我当前的BackupAgentHelper:
public class CustomBackupAgent extends BackupAgentHelper {
private static final String TAG = "FullBackupAgent";
private static final boolean DEBUG = true;
public void addFileHelper(String files[])
{
FileBackupHelper aHelper = new CustomFileHelper(this, files);
addHelper("userfiles", aHelper);
}
@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
ParcelFileDescriptor newState) throws IOException {
addFileHelper(getBackupDirs());
super.onBackup(oldState, data, newState);
}
private String[] getBackupDirs() {
LinkedList<File> dirsToScan = new LinkedList<>();
ArrayList<String> allFiles = new ArrayList<>();
// build the list of files in the app's /data/data tree
File file = new File(getFilesDir().getParentFile().getAbsolutePath());
dirsToScan.add(file);
Log.e(TAG, "Backing up dir tree @ " + file.getAbsolutePath() + " :");
while (dirsToScan.size() > 0) {
File dir = dirsToScan.removeFirst();
File[] contents = dir.listFiles();
if (contents != null) {
for (File f : contents) {
if (f.isDirectory()) {
dirsToScan.add(f);
} else if (f.isFile()) {
Log.e(TAG, " " + f.getAbsolutePath());
allFiles.add(f.getAbsolutePath());
}
}
}
}
return allFiles.toArray(new String[allFiles.size()]);
}
}
当我用&#34; adb shell bgmr run&#34;我得到以下logcat输出:
08-31 16:09:36.534 E/FullBackupAgent: Backing up dir tree @ /data/data/package :
08-31 16:09:36.542 E/FullBackupAgent: /data/data/package/cache/com.android.opengl.shaders_cache
08-31 16:09:36.542 E/FullBackupAgent: /data/data/package/shared_prefs/multidex.version.xml
08-31 16:09:36.550 E/FullBackupAgent: /data/data/package/code_cache/secondary-dexes/package.apk.classes2.dex
08-31 16:09:36.550 E/FullBackupAgent: /data/data/package/code_cache/secondary-dexes/package.apk.classes2.zip
08-31 16:09:36.550 D/BackupHelperDispatcher: handling existing helper 'userfiles' package.CustomFileHelper@41515378
08-31 16:09:36.557 I/PerformBackupTask: Backup pass finished.
08-31 16:11:38.776 V/BackupManagerService: Backup requested but nothing pending
当我重新安装应用程序时,我得到以下logcar输出:
08-31 16:30:13.932 D/BackupManagerService: MSG_RUN_RESTORE observer=android.app.backup.IRestoreObserver$Stub$Proxy@41b003c8
08-31 16:30:13.972 I/dalvikvm: Could not find method android.content.Context.getNoBackupFilesDir, referenced from method android.support.v4.content.ContextCompat.getNoBackupFilesDir
08-31 16:30:13.972 W/dalvikvm: VFY: unable to resolve virtual method 470: Landroid/content/Context;.getNoBackupFilesDir ()Ljava/io/File;
08-31 16:30:13.979 V/LocalTransport: ... key=com.android.sharedstoragebackup size=1107
08-31 16:30:14.065 V/BackupServiceBinder: doRestore() invoked
08-31 16:30:14.073 I/BackupManagerService: Restore complete.
但我备份的文件都没有恢复。我还尝试在可以从BackupHelperAgent覆盖的可用方法中记录或设置断点,但不会停止或登录其中任何一个。
AndroidManifest.xml如下所示:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="package">
<application android:allowBackup="true" android:label="@string/app_name"
android:backupAgent="CustomBackupAgent"
android:restoreAnyVersion="true"
android:supportsRtl="true">
<meta-data
android:name="com.google.android.backup.api_key"
android:value="xxx" />
</application>
</manifest>
如果您曾使用BackupAgent备份整个目录,请帮助:)
提前致谢!