在Android上,如何使用code。使用相同的库来区分的免费应用和付费应用 [英] On Android, how to use code to distinguish free app vs paid app using the same library

查看:117
本文介绍了在Android上,如何使用code。使用相同的库来区分的免费应用和付费应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现使用库付费VS免费这篇文章。它特别提到了以下内容:可能有指示支付布尔资源或自由的状态可以通过code库中的项目进行测试和功能打开或关闭相应通常情况下,库项目将有默认的支付状态资源和免费版本的价值将它覆盖到自由的价值。我究竟怎样才能使能够在免费版本中overrided一个布尔值?基本上,我该怎么办我的公共库code的东西,说,如果应用程序是免费的版本,那么显示广告?

I found this article on using a library for paid vs free. It specifically mentioned the following: "possible to have a boolean resource indicating paid or free status which can be tested by code in the library project and features switched on or off accordingly. Normally the library project would have the the default paid status value of the resource, and the free version would override it to the free value." How exactly can I make a boolean that can be overrided in the free version? Basically how can I do something in my common library code that says if app is free version then display ads?

推荐答案

做到这一点的方法之一是使用字符串资源文件 - 因为值为级联

One way to do this is by using the string resource file -- since the values are "cascading".

库code被复制到免费和付费的项目,并同时运行在该项目中的,这意味着它着眼于本地字符串资源第一的上下文。

The library code gets copied into both the free and paid projects and runs in the context of that project which means it looks at the "local" string resource first.

,它会编译但使用时所执行的更多的本土的价值。

As long as you also specify that string resource name in your library project, it will compile but use the more "local" value when it is executing.

在您的库项目,指定一个字符串,命名为有偿在res /价值/ strings.xml中

In your library project, specify a string named "paid" in res/values/strings.xml

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
    <string name="app_name">My app</string>
    <string name="paid">false</string>
</resources>

在您的免费项目,指定:

In your free project, specify:

<string name="paid">false</string>

在您的付费项目中,指定:

In your paid project, specify:

<string name="paid">true</string>

您现在可以访问该库中的项目:

You can now access this in your library project:

boolean paid = Boolean.parseBoolean(getString(R.string.paid));

当您运行免费的项目会报告错误,当你运行该项目支付会报告真。

When you run the free project it will report False and when you run the paid project it will report True.

然后你只需要检查布尔和分支了......

Then you just have to check that boolean and branch away...

if(!paid) {
  //show an ad (or whatever)
}

if(paid) {
  //load more game levels (or whatever)
}

这篇关于在Android上,如何使用code。使用相同的库来区分的免费应用和付费应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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