Android的共享preferences备份不工作 [英] Android SharedPreferences Backup Not Working

查看:288
本文介绍了Android的共享preferences备份不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做关于如何备份共享preferences在我的Andr​​oid应用程序,特别是使用反射来保持向后兼容性我的家庭作业。至少我一直在努力。不幸的是,没有我的code实际上结束了创建一个备份!这包括强制的亚行BMGR 的如下解释模拟器上的命令。所以我想,如果社会上也许可以帮助我,并在这个过程中想出一些更好的文档?

下面是我的code。为了保持尽可能通用别人,我就叫我的应用程序的刘德华与包名的 com.example.andy

Android清单(节选)

 <应用
    ...
    机器人:backupAgent =com.example.andy.backupHelper
    机器人:restoreAnyVersion =真正的>
    ...
    &所述;元数据
        机器人:名称=com.google.android.backup.api_key
        机器人:值=给定键GOES HERE/>
    ...
 

backupHelper.java

注:/data/data/com.example.andy/shared_$p$pfs/com.example.andy_$p$pferences.xml

 包com.example.andy;

进口android.app.backup.BackupAgentHelper;
进口android.app.backup.Shared preferencesBackupHelper;

公共类BlinkyBackup扩展BackupAgentHelper {

    静态最后弦乐preFS_FILE =andy_ preferences;
    静态最后弦乐BACKUP_KEY =安迪preferencesBackup;

    公共无效的onCreate(){

        共享preferencesBackupHelper backupHelper =新的共享preferencesBackupHelper(这一点,preFS_FILE);
        addHelper(BACKUP_KEY,backupHelper);
    }
}
 

BackupAgentWrapper

 包com.example.andy;

进口android.app.backup.BackupManager;
进口android.content.Context;

公共类BackupAgentWrapper {

    私人BackupManager wrappedInstance;

    静态{

        尝试 {

            的Class.forName(android.app.backup.BackupManager);
        }
        赶上(例外五){

            抛出新的RuntimeException(E);
        }
    }

    公共静态无效checkAvailable(){}

    公共无效dataChanged(){

        wrappedInstance.dataChanged();
    }

    公共BackupAgentWrapper(上下文CTX){

        wrappedInstance =新BackupManager(CTX);
    }
}
 

和最后,这些命令以启动在运行期间的备份。在我的应用程序,这code都可用一个类运行我的应用程序(不是主要的活动),这是通过作为创建时的上下文,然后存储在私有变量mContext。

 私人无效backupData(){

    布尔backupAgentAvailable = FALSE;

    尝试 {

        BackupAgentWrapper.checkAvailable();
        backupAgentAvailable = TRUE;
    }
    捕获(的Throwable T){

        //真的无关
    }

    如果(backupAgentAvailable){

        BackupAgentWrapper backupWrapper =新BackupAgentWrapper(mContext);
        backupWrapper.dataChanged();
    }
}
 

总之,无论是上述函数也不低于实际备份所有数据的命令:

  $亚行外壳BMGR实现真正的
$亚行外壳BMGR备份com.example.andy
$亚行外壳BMGR运行
 

解决方案

在主呼召的活动(第一个,在你的应用程序启动),您需要实例 BackupManager

  BackupManager mBackupManager =新BackupManager(getApplicationContext());
 

这将告诉backupmanager查找备份文件并加载它。

您需要确保在preferences文件格式packagename_ preferences如。 andy_ preferences。并使用相同的名称,当你第一次保存在preferences。 (很重要!)

在通过的设置活动节省应用()提交(),你需要告诉 BackupManager 的东西已经改变,所以包括后立即:

  mBackupManger.dataChanged();
 

I've been doing my homework on how to backup SharedPreferences in my Android application, especially using reflection to maintain backwards compatibility. At least I've been trying. Unfortunately, none of my code actually ends up creating a backup! This includes forcing adb bmgr commands on the emulator as explained here. So I'm wondering if the community could perhaps help me out and in the process come up with some better documentation?

Here's my code. To keep this as generic as possible for others, I will simply call my application Andy with a package name of com.example.andy.

Android Manifest (excerpt)

<application
    ...
    android:backupAgent="com.example.andy.backupHelper"
    android:restoreAnyVersion="true">
    ...
    <meta-data
        android:name="com.google.android.backup.api_key"
        android:value="GIVEN KEY GOES HERE" />
    ...

backupHelper.java

Note: /data/data/com.example.andy/shared_prefs/com.example.andy_preferences.xml

package com.example.andy;

import android.app.backup.BackupAgentHelper;
import android.app.backup.SharedPreferencesBackupHelper;

public class BlinkyBackup extends BackupAgentHelper {

    static final String PREFS_FILE = "andy_preferences";
    static final String BACKUP_KEY = "AndyPreferencesBackup";

    public void onCreate() {

        SharedPreferencesBackupHelper backupHelper = new SharedPreferencesBackupHelper(this, PREFS_FILE);
        addHelper(BACKUP_KEY, backupHelper);
    }
}

BackupAgentWrapper

package com.example.andy;

import android.app.backup.BackupManager;
import android.content.Context;

public class BackupAgentWrapper {

    private BackupManager wrappedInstance;

    static {

        try {

            Class.forName("android.app.backup.BackupManager");
        }
        catch (Exception e) {

            throw new RuntimeException(e);
        }
    }

    public static void checkAvailable() {}

    public void dataChanged() {

        wrappedInstance.dataChanged();
    }

    public BackupAgentWrapper(Context ctx) {

        wrappedInstance = new BackupManager(ctx);
    }
}

And finally, the commands to initiate a backup during run-time. In my application, this code is run from a class available to my application (not the main activity) which is passed this as a context upon creation and then stored in the private variable mContext.

private void backupData() {

    boolean backupAgentAvailable = false;

    try {

        BackupAgentWrapper.checkAvailable();
        backupAgentAvailable = true;
    }
    catch (Throwable t) {

        // really nothing to do
    }

    if(backupAgentAvailable) {

        BackupAgentWrapper backupWrapper = new BackupAgentWrapper(mContext);
        backupWrapper.dataChanged();
    }
}

To summarize, neither the above function nor the commands below actually backup any data:

$ adb shell bmgr enable true
$ adb shell bmgr backup com.example.andy
$ adb shell bmgr run

解决方案

In your main calling activity (first one that starts in your app), you need to instantiate BackupManager:

BackupManager mBackupManager = new BackupManager(getApplicationContext());

This will tell the backupmanager to look for the backup file and load it.

You need to make sure the preferences file is format "packagename_preferences" eg. andy_preferences. And use the same name when you first saved your preferences. (Very important!)

After the your settings activity saves via apply() or commit(), you need to tell BackupManager that something has changed so include immediately after:

mBackupManger.dataChanged();

这篇关于Android的共享preferences备份不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆