Android主项目与库项目-如何在项目之间传递设置
刚刚开始使用 Android,我正在尝试创建一个具有免费和付费版本的应用程序。我也是 Java 新手,但我成功地在 Eclipse 中创建了一个简单的工作应用程序,它由 2 个主要项目组成(一个是免费版本,一个是付费版本)。
我还有一个库项目,其中包含共享代码(活动、资源、字符串等)并由主项目引用。我想要做的(这很可能是错误的方法)是根据我运行的是免费版本还是付费版本来启用或禁用库代码中的内容。
例如,我在图书馆项目中有一个主要活动,我想做一些类似的事情:
if (version == "free") //version would somehow be set by the main project
{
//disable a paid feature
}
显然,我需要以某种方式在图书馆中确定我正在运行免费还是付费主项目,以便我可以在我运行时启用/禁用功能需要。执行此操作的最佳方法是什么?例如,我可以在主项目中使用某种全局设置,并以某种方式在库项目中读取它 - 我不希望库项目了解主项目,如果这有意义的话?
我学得很快,所以请随时为我指出任何文章的方向等。
Just started playing with Android and I'm trying to create an app that has free and paid version. I'm new to Java as well but I've managed to create a simple working application in Eclipse which consists of 2 main projects (one for the free version, and one for the paid version).
I also have a library project which contains shared code (activities, resources, strings etc) and is referenced by the main projects. What I want to do (and this may well be the wrong approach) is enable or disable things in the the library code depending on whether I'm running the free or paid version.
So for example I have a Main activity in the library project and I want to do something like:
if (version == "free") //version would somehow be set by the main project
{
//disable a paid feature
}
Obviously I need to somehow work out in the library whether I'm running the free or paid main project so I can enable/disable features when I need to. What's the best approach to do this? For example could I use some kind of global setting in the main project, and somehow read this in the library project - I don't want the library project to know about the main project, if that makes sense?
I'm a quick study so feel free to point me in the direction of any articles, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码有效,但如果您使用相同的 apk 同时具有免费和付费(解锁)功能,我不确定将付费/免费标志存储在清单中是否是最佳解决方案。相反,我会通过在库项目中添加一个方法来实现此目的,该方法允许任何项目设置应用程序是免费还是付费的标志。
ex 在库项目中:
在您的库中,您可以检查此标志:
因此,在您的主项目中,如果应用程序已付费:
Your code works, but I'm not sure storing your paid/free flag in your manifest is the best solution if you are using the same apk to have both free and paid (unlock) functionality. Instead I would do this by adding a method in the library project which allows any project to set a flag on whether the app is free or paid.
ex in library project:
and in your library, you can check vs this flag:
So in your main project, if the app has been paid for:
好的,我设法解决了这个问题。在应用程序清单中,我只需设置 android:label 属性并使用下面的代码引用它。
在清单中:
引用此
OK I managed to solve this. In the Application Manifest, I simply set the android:label property and referenced this using the code below.
In Manifest:
To reference this