简体   繁体   English

BackupAgent:“无法恢复包...”

[英]BackupAgent: “unable to restore package …”

I have implemented the BackupAgent as described at Data Backup , registered an API key and declared the BackupAgent in my Manifest. 我按照数据备份中的描述实现了BackupAgent,注册了一个API密钥并在我的Manifest中声明了BackupAgent。 The Backup part work's quite well, I think; 我认为备份部分的工作非常好; When I run adb shell bmgr run in command line, the following output appears in LogCat: 当我在命令行中运行adb shell bmgr run时,LogCat中会出现以下输出:

01-11 22:23:09.002: DEBUG/PerformBackupThread(97): starting agent for backup of BackupRequest{app=ApplicationInfo{4547c5b8 com.meins.nightclock} full=false}
01-11 22:23:09.002: DEBUG/BackupManagerService(97): awaiting agent for ApplicationInfo{4547c5b8 com.meins.nightclock}
01-11 22:23:09.013: DEBUG/BackupManagerService(97): agentConnected pkg=com.meins.nightclock agent=android.os.BinderProxy@4536a7f8
01-11 22:23:09.032: DEBUG/BackupHelperDispatcher(9122): handling existing helper 'alarms' android.app.backup.FileBackupHelper@44e197b0
01-11 22:23:09.032: DEBUG/BackupHelperDispatcher(9122): handling existing helper 'prefs' android.app.backup.SharedPreferencesBackupHelper@44e19478
01-11 22:23:09.032: VERBOSE/LocalTransport(97): performBackup() pkg=com.meins.nightclock
01-11 22:23:09.032: VERBOSE/LocalTransport(97): Got change set key=alarms:alarms size=16 key64=YWxhcm1zOmFsYXJtcw==
01-11 22:23:09.042: VERBOSE/LocalTransport(97):   data size 16
01-11 22:23:09.062: VERBOSE/LocalTransport(97): Got change set key=prefs:com.meins.nightclock_preferences size=265 key64=cHJlZnM6Y29tLm1laW5zLm5pZ2h0Y2xvY2tfcHJlZmVyZW5jZXM=
01-11 22:23:09.072: VERBOSE/LocalTransport(97):   data size 265
01-11 22:23:09.072: VERBOSE/LocalTransport(97): finishBackup()

The restore part on the other hand doesn't work at all, on onReceive() method is not called. 另一方面,恢复部分根本不起作用, onReceive()方法不会被调用。 When I reinstall my app, the only output which may (?) refer to the BackupAgent is 当我重新安装我的应用程序时,唯一可能(?)引用BackupAgent的输出是

01-11 22:14:01.042: DEBUG/vending(7426): [100] LocalAssetCache.updateOnePackage(): No local info for com.meins.nightclock

When I run adb shell bmgr restore com.meins.nightclock it simply states Unable to restore package com.meins.nightclock . 当我运行adb shell bmgr restore com.meins.nightclock它只是声明Unable to restore package com.meins.nightclock


I am using the following implementation of BackupAgentHelper 我正在使用BackupAgentHelper的以下实现

package com.meins.nightclock;

import java.io.IOException;

import android.app.backup.BackupAgentHelper;
import android.app.backup.BackupDataInput;
import android.app.backup.BackupDataOutput;
import android.app.backup.FileBackupHelper;
import android.app.backup.SharedPreferencesBackupHelper;
import android.os.ParcelFileDescriptor;
import android.util.Log;

public class MyBackupAgent extends BackupAgentHelper {

    public static final Object[] DATA_LOCK = new Object[0];

    private static final String PREFS_BACKUP_KEY = "prefs";
    private static final String ALARMS_BACKUP_KEY = "alarms";

    private DataLayerAlarms d;

    @Override
    public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
            ParcelFileDescriptor newState) throws IOException {

        if (d.backup() != 0)
            return;

        synchronized (DATA_LOCK) {
            super.onBackup(oldState, data, newState);
        }
    }

    @Override
    public void onCreate() {
        SharedPreferencesBackupHelper preferencesHelper = new SharedPreferencesBackupHelper(this,
                getPackageName() + "_preferences");
        addHelper(PREFS_BACKUP_KEY, preferencesHelper);

        FileBackupHelper fileHelper = new FileBackupHelper(this, DataLayerAlarms.BACKUP_FILE);
        addHelper(ALARMS_BACKUP_KEY, fileHelper);

        d = new DataLayerAlarms(this);
    }

    @Override
    public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
            throws IOException {
        Log.d(toString(), "onRestore()");
        synchronized (DATA_LOCK) {
            super.onRestore(data, appVersionCode, newState);
        }

        d.restore();
    }

}

For the sake of completeness the relevant Manifest part: 为了完整起见,相关的Manifest部分:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:debuggable="true"
    android:backupAgent="MyBackupAgent">
    <meta-data
        android:name="com.google.android.backup.api_key"
        android:value="AEdPqrEAAAAI4MsUOC-[...]" />

    ...

</application>

Does anyone know, why the BackupManager is unable to restore this package? 有谁知道,为什么BackupManager无法恢复此包?

Try adding a dot before your backup class name, as in: 尝试在备份类名称前添加一个点,如下所示:

android:backupAgent=".MyBackupAgent"

Or if your backup class isn't in your root package, add the path to it: 或者,如果备份类不在根包中,请添加其路径:

android:backupAgent=".package.MyBackupAgent"

android:backupAgent="MyBackupAgent"更改为android:backupAgent=".MyBackupAgent"也为我工作。

This problem happened when I changed the package name, and then other problems subsequently appeared. 当我更改包名称时会发生此问题,然后出现其他问题。 It took me a couple hours to figure out. 我花了几个小时才搞清楚。

Disable Instant Run. 禁用即时运行。 It messed up things for me. 这对我来说搞砸了。

Check the transports list, make sure Google is set: 检查传输列表,确保已设置Google:

adb shell bmgr list transports
    android/com.android.internal.backup.LocalTransport
  * com.google.android.gms/.backup.BackupTransportService

Make sure the BackupAgentHelper package name and the meta-data package name have the same name. 确保BackupAgentHelper软件包名称和元数据包名称具有相同的名称。 The name conflicts could cause a problem. 名称冲突可能会导致问题。

android:backupAgent The name of the class that implement's the application's backup agent, a subclass of BackupAgent. android:backupAgent实现应用程序备份代理的类的名称,BackupAgent的子类。 The attribute value should be a fully qualified class name (such as, "com.example.project.MyBackupAgent"). 属性值应该是完全限定的类名(例如,“com.example.project.MyBackupAgent”)。 However, as a shorthand, if the first character of the name is a period (for example, ".MyBackupAgent"), it is appended to the package name specified in the element. 但是,作为简写,如果名称的第一个字符是句点(例如,“。MyBackupAgent”),则它将附加到元素中指定的包名称。 There is no default. 没有默认值。 The name must be specified. 必须指定名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM