OnRestore中没有为我的自定义BackupAgent调用为我、自定义、OnRestore、BackupAgent

2023-09-05 05:27:21 作者:有深度的,有深度有内涵的

有在我的Andr​​oid应用程序的一些数据,我想备份和恢复。为此目的,我创建了一个自定义实现的 BackupAgent

There are some data in my Android app that I would like to backup and restore. For that purpose I have created a custom implementation of BackupAgent.

在我的已经包括了备份代理表现为你可以看到下面

In my manifest in have included the backup agent as you can see below

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:installLocation="auto"
    package="com.myapp"
    android:versionCode="14"
    android:versionName="1.13" >

    <application
        android:backupAgent="com.myapp.MyBackupAgent">

        <meta-data
            android:name="com.google.android.backup.api_key"
            android:value="my key" />

我已经包括了备份服务API密钥,虽然我用的仿真器(安卓2.2)测试,它不应该是必要的,因为它使用本地备份传输。

I have included the backup service api key, although I am testing with the emulator (Android 2.2) and it should not be necessary, because it uses the local backup transport.

为了做备份的测试和恢复我也做了以下内容:

In order to do the test of the backup and restore I have done the following:

在开始安装我的应用程序模拟器。

启用备份 Start the emulator with my application installed.

Enable backup

亚行外壳BMGR实现真正的

adb shell bmgr enable true

呼叫我的code组在BackupManager类的dataChanged方法被调用的部分。

Call the part of my code were the dataChanged method in the BackupManager class is called.

启动手动备份操作

亚行外壳BMGR运行

经过的日志我的自定义BackupAgent的onBackup方法称为

Checked in the log that the onBackup method of my custom BackupAgent was called.

的事情是OnRestore中的方法似乎并不被调用,我不知道为什么。重新安装应用程序或手动触发亚行我看到下面的控制台。恢复后

The thing is that the onRestore method does not seem to be called and I don't know why. After reinstalling the app or manually triggering the restore with adb I see the following in the console.

$adb shell bmgr restore com.myapp
restoreStarting: 2 packages
restoreFinished: 0
done

和这个其他的日志中

D/AndroidRuntime( 8259):
D/AndroidRuntime( 8259): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
D/AndroidRuntime( 8259): CheckJNI is ON
D/AndroidRuntime( 8259): --- registering native functions ---
D/BackupManagerService(   59): MSG_RUN_RESTORE observer=android.app.backup.IRestoreObserver$Stub$Proxy@450e16a8
V/LocalTransport(   59): start restore 1
V/LocalTransport(   59):   nextRestorePackage() = @pm@
V/LocalTransport(   59):   getRestoreData() found 7 key files
V/LocalTransport(   59):     ... key=com.android.providers.settings size=1208
V/LocalTransport(   59):     ... key=com.myapp size=501
V/LocalTransport(   59):     ... key=android size=1208
V/LocalTransport(   59):     ... key=com.android.providers.userdictionary size=1208
V/LocalTransport(   59):     ... key=com.android.browser size=1208
V/LocalTransport(   59):     ... key=com.android.inputmethod.latin size=1208
V/LocalTransport(   59):     ... key=@meta@ size=11
V/LocalTransport(   59):   no more packages to restore
V/LocalTransport(   59): finishRestore()
V/LocalTransport(   59): finishRestore()
D/AndroidRuntime( 8259): Shutting down VM

但我不认为只是在它的开头调用OnRestore中实际上是由(我有一些日志语句?

but I don't see that the call to onRestore is actually made (I have some logging statements just at the beginning of it?

有没有这个不断发生在你身上?有什么理由用OnRestore中的方法不叫,甚至在 onBackup 被称为?

Has this ever happen to you? Is there any reason by the onRestore method is not called, even in the onBackup was called?

推荐答案

我有这个问题的根本原因是,我有一个错误,在我的onBackup $ P $的完成pventing,所以onBackup被称为但是OnRestore中不。固定的异常onBackup造成OnRestore中被调用。

I had this problem and the root cause was that I had a bug in my onBackup preventing it from completing, so onBackup was called but onRestore was not. Fixing the exception in onBackup caused onRestore to be called.

另外登录onBackup和OnRestore中的信息也显示在日志中。如果您设置的标签备份日志过滤器,并使用一些与备份为您的日志标签。你会看到从系统和你的记录。下面是我所得到的

Also Log messages in onBackup and onRestore do show up in the logs. If you set a log filter on the tag "backup" and use something with backup in it for your log tag. You will see logging from the system and yours. Here is what I get

09-08 17:06:56.581      294-352/system_process V/BackupServiceBinder﹕ doBackup() invoked
09-08 17:06:56.591      294-352/system_process D/PerformBackupTask﹕ starting agent for backup of BackupRequest{pkg=android}
09-08 17:06:56.591      294-352/system_process D/BackupManagerService﹕ awaiting agent for ApplicationInfo{40d5efc0 android}
09-08 17:06:56.591      294-308/system_process D/BackupManagerService﹕ agentConnected pkg=android agent=android.app.backup.BackupAgent$BackupServiceBinder@4112a228
09-08 17:06:56.601      294-352/system_process V/BackupServiceBinder﹕ doBackup() invoked
09-08 17:06:56.601      294-352/system_process D/BackupHelperDispatcher﹕ handling existing helper 'wallpaper' android.app.backup.WallpaperBackupHelper@41149150
09-08 17:06:56.621      294-352/system_process D/PerformBackupTask﹕ starting agent for backup of BackupRequest{pkg=com.catglo.sellpr}
09-08 17:06:56.661      294-352/system_process D/BackupManagerService﹕ awaiting agent for ApplicationInfo{41074748 com.catglo.sellpr}
09-08 17:06:56.781      294-514/system_process D/BackupManagerService﹕ agentConnected pkg=com.catglo.sellpr agent=android.os.BinderProxy@410768c8
09-08 17:06:56.791    2263-2274/com.catglo.sellpr V/BackupServiceBinder﹕ doBackup() invoked
09-08 17:06:56.791    2263-2274/com.catglo.sellpr I/backup﹕ onBackup called
09-08 17:06:57.251      294-352/system_process I/PerformBackupTask﹕ Backup pass finished.

在上面的日志com.catglo.sellpr是从我的应用程序和读的 com.catglo.sellpr I /备份线路:onBackup名为的是在我的code中的日志信息。对于OnRestore中,我得到

in the above log com.catglo.sellpr is from my app and the line that reads com.catglo.sellpr I/backup﹕ onBackup called is the log message in my code. For the onRestore I get

09-08 17:13:34.431      294-352/system_process D/BackupManagerService﹕ MSG_RUN_RESTORE observer=android.app.backup.IRestoreObserver$Stub$Proxy@413132c0
09-08 17:13:34.511      294-352/system_process V/BackupServiceBinder﹕ doRestore() invoked
09-08 17:13:34.561      294-352/system_process D/BackupManagerService﹕ awaiting agent for ApplicationInfo{41074748 com.catglo.sellpr}
09-08 17:13:34.561      294-427/system_process D/BackupManagerService﹕ agentConnected pkg=com.catglo.sellpr agent=android.os.BinderProxy@41127ee0
09-08 17:13:34.571    2263-2276/com.catglo.sellpr V/BackupServiceBinder﹕ doRestore() invoked
09-08 17:13:34.571    2263-2276/com.catglo.sellpr I/backup﹕ onRestore called

早些时候,我曾在onBackup异常和OnRestore中我的日志从来没有所谓,但要恢复相关的系统信息是。

Earlier I had an exception in onBackup and my log from onRestore was never called but the system messages related to restore were.

由于在备份异常的应用程序将不会强制关闭。

The app will not Force Close because of an exception in the backup.