2

两天来,我试图让 BackupAgent/BackupAgentHelper 在低于 6.0 的 Android 版本上工作。我的目标是通过 com.google.android.backup/.BackupTransportService 或 android/com.android.internal.backup.LocalTransport 备份 /data/data/com.package/ 文件夹中的所有文件。

是否有关于存储多个文件的 BackupAgentHelper 的默认实现的教程?我已经尝试将 FileBackupHelper 与我在 Internet 上可以找到的每个片段一起使用,但是这些文件无法恢复。

我当前的 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()]);
    }

}

当我使用“adb shell bgmr run”触发备份时,我得到以下 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 备份整个目录,请帮助 :)

提前致谢!

4

0 回答 0