与存储在库共享preferences处理 [英] Dealing with shared preferences stored in a library

查看:171
本文介绍了与存储在库共享preferences处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了一个免费的应用程序和付费版本。我把所有的免费版本code到库中,这是我从一个新的免费版本项目中引用,而付费版本的项目。

I have an app that has a free, and a paid version. I put all the free version code into a library, which I reference from a new free version project, and the paid version project.

我注意到,图书馆,现在我的项目都有一个属性文件,我的preferences完全停止工作。如此看来,这取决于code执行什么命名空间中,它得到了不同的结果,当你查询共享preferences。

I noticed that the library, and now my projects all have a properties file, and my preferences stopped working completely. It seems that depending on what namespace the code executes in, it gets a different result when you query for SharedPreferences.

什么是对付它的最好方法?这两个应用程序具有相同的preferences截至目前,但我可能会延长后付费版本,并补充说,没有在免费版本中存在preferences。

What is the best way to deal with this? Both apps have the same preferences as of now, but I will probably extend the paid version later and add preferences that don't exist in the free version.

推荐答案

我假设你是问是否您的库可与顶级应用程序共享preFS,不在于是否免费版本可以与完整版分享。

I assume you are asking whether your library can share prefs with the top level app, not whether the free version can share with the full version.

API文档提到,你不能共享一个给定的程序之外的preFS,所以是有可能,因为你(可能)有两种不同的封装(一个用于lib中,一个顶层),该preFS您在库VS顶级应用得到来自不同的文件吗?

The api docs mention that you can't share prefs outside of a given process, so is it possible that since you (probably) have two different packages (one for lib, one for top-level), the prefs you get in the library vs the top level app are from different files?

您可以通过浏览您的手机存储在/数据/数据​​/你的包为顶级应用程序和库preference文件检查。你可能会发现,你对每个不同的preference文件,我认为这意味着你不能在库/应用程序之间共享preFS。

You can check this by browsing your phone storage in /data/data/your-package for the preference file for the top level app and the library. You might find that you have a different preference file for each, which I think means that you can't share prefs between the library / app.

您可能能够通过只读,并从顶层应用程序写入到preFS,并创建一个接口来提供这些preFS图书馆绕过这些限制。如果您的图书馆只是美元,从顶层的值psented p $,则该库将无法访问preFS,因此所有的preference访问是从一个地方完成。

You might be able to get around these limitations by only reading and writing to prefs from the top level app, and creating an interface to deliver those prefs to the library. If your library is simply presented with the values from the top level, then the library won't have to access the prefs, and thus all your preference access is done from a single place.

此外,这将让你有不同的preferences完全与自由(这是默认的行为,无论如何,因为它们会被存储在单独的preference文件),然后就可以发送正确的到基于版本的lib信息。

Also, this will allow you to have different preferences for full vs. free (which is the default behavior anyway, since they will be stored in separate preference files), and then you can send the proper info down to the lib based on version.

这篇关于与存储在库共享preferences处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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