自定义路径的 SharedPreferences - Android [英] SharedPreferences to a custom path - Android

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

问题描述

我想将我的 SharedPreferences xml 保存在默认路径是应用程序包的不同位置.

I want to keep my SharedPreferences xml in a different place where default path is the application package.

我们可以为 android SharedPreferences 设置自定义路径并在该路径中使用它吗?

Can we set a custom path for android SharedPreferences and use it while it is in that path ?

推荐答案

您可以创建一个适配器来获取首选项.

You can create a adapter to get preferences.

首先,创建一个接口IPreferences(不是必须的,但如果你想把它改回来或改成另一种方法,会让你的生活更轻松):

First, create an interface IPreferences (Not necessary but make your life easier if you want to change it back or change to another method):

public interface IPreferences {
    boolean contains(String key);
    int getInt(String key, int defValue);
    String getString(String key, String defValue);
    void putInt(String key, int value);
    void putString(String key, String value);
}

(有些方法被省略了,大部分都是多余的)

(Some methods are left out, most of them are redundant)

然后,您使用 xml 存储类 XMLPreferences 来实现.

Then, you implement with a xml storage class XMLPreferences.

public class XMLPreferences implements IPreferences {
    private final String path;

    public XMLPreferences(String path) {
        this.path = path;
    }

    @Override
    public boolean contains(String key) {
        return getContentByKey(key) == null;
    }

    @Override
    public int getInt(String key, int defValue) {
        if( getContentByKey(key) == null)
            return defValue;
        return Integer.valueOf(key);
    }

    @Override
    public String getString(String key, String defValue) {
        if( getContentByKey(key) == null)
            return defValue;
        return defValue;
    }

    @Override
    public void putInt(String key, int value) {
        putContentByKey(key, value);
    }

    @Override
    public void putString(String key, String value) {
        putContentByKey(key, value);
    }

    private String getContentByKey(String key) {
        try {
            FileInputStream fileInputStream = new FileInputStream(path);
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document dom = builder.parse(fileInputStream);
            Element root = dom.getDocumentElement();
            NodeList nodes = root.getElementsByTagName(key);
            if (nodes.getLength() > 0)
                return nodes.item(0).getTextContent();
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private void putContentByKey(String key, String content) {
        try {
            FileInputStream fileInputStream = new FileInputStream(path);
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document dom = builder.parse(fileInputStream);
        Element root = dom.getDocumentElement();
            NodeList nodes = root.getElementsByTagName(key);
            if (nodes.getLength() > 0)
                nodes.item(0).setTextContent(content);
            else {
                Element newElement = dom.createElement(key);
                newElement.setTextContent(content);
                root.appendChild(newElement);
            }
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            Result output = new StreamResult(new File(path));
            Source input = new DOMSource(dom);
            transformer.transform(input, output);
        } catch (ParserConfigurationException | SAXException | IOException | TransformerException e) {
            e.printStackTrace();
        }
    }
}

最后,您可以调用以下内容:

Finally, you can call the following:

IPreferences pref = new XMLPreferences(YOUR_PATH);

以后如果想改变实现,可以用IPreferences实现一个新的类.然后,您可以只更改初始化而不更改其他部分.

Later, if you want to change the implementation, you can implement a new class with IPreferences. Then, you can just change the initialization without changing other parts.

附言这使用 DOM Praser 如果你有一个可能会面临性能问题大型 XML 文件.您可能想改用其他 XML Praser

P.S. This uses DOM Praser which may facing performance issue if you have a large XML file. You may want use other XML Praser instead

这篇关于自定义路径的 SharedPreferences - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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