Android备份ENTIRE SharedPreferences文件 [英] Android Backup of ENTIRE SharedPreferences file

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

问题描述

我一直在尝试在我的Android应用程序中备份我的sharedpreferences文件,但到目前为止还没有。我正在使用开发人员指南中的简单Google代码。下面是 MyPrefsBackup 类的代码。

I have been trying to get the backup of my sharedpreferences file in my Android app working and so far it is not. I am using the simple Google code from the developer's guide. Below is the code for the MyPrefsBackup class.

public class MyPrefsBackup extends BackupAgentHelper {

    // The name of the SharedPreferences file

    static final String PREFS = "UserDB";

    // A key to uniquely identify the set of backup data

    static final String PREFS_BACKUP_KEY = "prefs";

    // Allocate a helper and add it to the backup agent

  public  void onCreate() {

        SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS);
        addHelper(PREFS_BACKUP_KEY, helper);


    }

我想我终于意识到了 PREFS_BACKUP_KEY 实际上必须是我用来存储数据的特定键。我只是使用偏好设置,所以我认为这就是为什么没有数据被备份的原因。但是,我在 SharedPreferences 文件中存储了很多数据,所以我该如何继续保存整个 SharedPreferences 文件,但未指定每个单独的密钥。 (某些密钥是由应用程序生成的,因此在用户输入数据之前,我什至不知道它们是否正在使用中)。

I think I have finally realized that the PREFS_BACKUP_KEY actually has to be the specific key that I stored the data under. I was just using "prefs" so I think that's why no data was being backed up. However, I am storing quite a bit of data in the SharedPreferences file so how can I go ahead and save the entire SharedPreferences files without specifiying every individual key. (some keys are generated by the app so I don't even know if they are in use until the user inputs their data).

我想知道是否有一种方法可以告诉 BackupHelper 类备份整个 SharedPreferences 文件?

I want to know is there a way to just tell the BackupHelper class to backup the entire SharedPreferences file?

推荐答案

示例SharedPreferences

Example SharedPreferences

SharedPreferences preferences=getSharedPreferences("Test", getApplicationContext().MODE_MULTI_PROCESS)

备份文件夹的日期

Calendar c = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("dd_MMMM_yyyy HH_mm_ss");
            String strDate = sdf.format(c.getTime());

导出共享首选项

exportSH("Test.xml", strDate);

将文件夹中的SharedPreferences .xml文件导出到下载目录

Exporting the SharedPreferences .xml File in a Folder to the Downloads directory

private void exportSH(String sh_name, String strDate){




        File sd = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + 
                File.separator + "Backup Folder "+strDate+ 
                File.separator );

          boolean success = true;
           if (!sd.exists()) {
               success = sd.mkdir();
           }
           if (success) {

        File data = Environment.getDataDirectory();
       FileChannel source=null;
       FileChannel destination=null;
       String currentDBPath = "/data/"+ context.getPackageName() +"/shared_prefs/"+ sh_name;
       String backupDBPath = sh_name;
       File currentDB = new File(data, currentDBPath);
       File backupDB = new File(sd, backupDBPath);
       try {
            source = new FileInputStream(currentDB).getChannel();
            destination = new FileOutputStream(backupDB).getChannel();
            destination.transferFrom(source, 0, source.size());
            source.close();
            destination.close();
            Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
        } catch(IOException e) {
            e.printStackTrace();
Toast.makeText(this, "Failed", Toast.LENGTH_SHORT).show();
        }
           }}

您可以导入文件进行较小的更改。有人说它不会那么容易,但确实可以。只需确保在导入之前创建一个空的Sharedpreferences文件,否则它将无法正常工作。

You can import the Files with small changes. Some people said that it won't work so easily but it does. Just make sure you create a empty Sharedpreferences File before you import, because otherwise it doesn't work.

例如,您可以这样做

    SharedPreferences dummy = getSharedPreferences("dummy", getApplicationContext().MODE_MULTI_PROCESS);
                Editor editor = dummy.edit();
editor.putBoolean("asdf", true);

    editor.commit();

如果在使用SharedPreferences的相同布局上导入它,则可以使用它刷新

And if you import it on the same Layout where the SharedPreferences are used, you can use this to refresh

                        finish();
                        startActivity(getIntent());

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

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