如何在Android应用程序中实现应用内计费?
看来在Android应用程序中实现应用内计费是相当复杂的。我怎么能这样做呢? SDK 中的示例应用程序只有一个 Activity,这对于像我这样具有多个 Activity 的应用程序来说过于简化了。
It seems that it is quite complicated to implement In-App Billing in an Android app. How could I do this? The sample app from the SDK only has one Activity, which kind of over-simplifies it for an application like mine that has multiple Activities.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
好吧,我会尝试解释一下我的经历。我不认为自己是这方面的专家,但几天来我伤透了脑筋。
对于初学者来说,我在尝试理解示例和应用程序的工作流程时经历了一段非常糟糕的时光。我认为最好从一个简单的示例开始,但是将代码分成小块并且不知道是否破坏了任何内容非常困难。我将告诉您我拥有什么以及我对示例进行了哪些更改以使其正常工作。
我有一个活动,我的所有购买都来自其中。它被称为专业版。
首先,您应该使用公共市场开发人员密钥更新 Security 类中的变量 base64EncodedPublicKey,否则您将看到一个不错的异常。
好吧,我将 Activity 绑定到 BillingService,如下所示:
这样,所有购买都会与该服务通信,然后该服务会将 JSON 请求发送到市场。您可能认为购买是在同一时刻进行的,但事实并非如此。您发送请求,购买可能会在几分钟或几小时后完成。我认为这主要是服务器过载和信用卡审批问题。
然后我有一个包含我的商品的 ListView,并在每个商品上打开一个 AlertDialog,邀请他们购买该商品。当他们单击某个项目时,我会执行以下操作:
好吧,您应该看到市场打开,并且用户完成或取消购买。
那么重要的是我的 PurChaseObserver,它处理市场发送的所有事件。这是它的精简版本,但您应该明白要点(通过代码查看我的评论):
而且我相信您不需要编辑其他任何内容。其余代码“有效”。
您可以首先在自己的项目“android.test.purchased”中尝试使用示例 SKU。到目前为止,我已经对此进行了测试并且它有效,但是我仍然需要涵盖诸如退款状态之类的所有内容。在这种情况下,我让用户保留这些功能,但我想在修改它之前确保它完美运行。
我希望它对您和其他人有所帮助。
Well, I'll try to explain what I experienced. I don't consider myself an expert on this but I broke my head several days.
For starters, I had a very bad time trying to understand the workflow of the example and the application. I thought it should be better to start with a simple example however its much difficult to separate the code in small pieces and not knowing if you are breaking anything. I'll tell you what I have and what I changed from the example to make it work.
I have a single Activity where all my purchases come from. It's called Pro.
First, you should update the variable base64EncodedPublicKey in your Security class with your public Market developer key or you will see a nice Exception.
Well, I bind my Activity to my BillingService like so:
That way, all the purchases talk to this service, that will then, send the JSON requests to the market. You might think that the purchases are made on the same instant but no. You send the request and the purchase might come minutes or hours later. I think this is mainly to server overload and approval of the credit cards.
Then I have a ListView with my items, and I open a AlertDialog on each one, inviting them to buy the item. When they click on an item, I do this:
Alright, you should see that the Market opens and the user either finishes or cancels the buy.
Whats then important is my PurChaseObserver, which handles all the events that market sends. This is a stripped version of it but you should get the point (See my comments throught the code):
And I believe you shouldn't need to edit anything else. The rest of the code "works".
You can try using the sample SKU at first in your own items "android.test.purchased". So far I have tested this and it works however I still need to cover everything like the refunded state. In this case, I am letting the user keep the features but I want to make sure it works perfect before modyfing it.
I hope it helps you and others.
V3:这是一个快速入门教程。他正在使用 google 示例中的帮助程序类(Trivial Drive)...就像第一个“Hello Billing”一样好..
http://www.techotopia.com/index.php/Integrating_Google_Play_In-app_Billing_into_an_Android_Application_%E2%80% 93_A_教程
V3: here is an tutorial for a quick start.. He´s using the helper-classes from the google example (Trivial Drive) ... Good as first "Hello Billing" ..
http://www.techotopia.com/index.php/Integrating_Google_Play_In-app_Billing_into_an_Android_Application_%E2%80%93_A_Tutorial
这里有一个 Android In-App Billing v3 的完整示例,并附有屏幕截图。请查看教程:
使用 ServiceConnection 的 Android 应用内结算 v3类
希望能有所帮助。
如需更多说明,请阅读本教程:在版本 3 API 中实现应用内结算< /a>
在我们的项目中集成应用内计费库的步骤
更新您的 AndroidManifest.xml 文件。
创建 ServiceConnection 并将其绑定到 IInAppBillingService。
将应用程序内计费请求从您的应用程序发送到 IInAppBillingService。
处理来自 Google Play 的应用内结算响应。
更新AndroidManifest.xml
在Manifest.xml文件中添加权限
将 AIDL 文件添加到您的项目中
构建您的应用程序。您应该在项目的 /gen 目录中看到一个名为 IInAppBillingService.java 的生成文件。
更新 build.gradle 文件
InAppPurchaseActivity.java 和 Activity_in_app_purchase.xml
中的依赖项这将为我们的应用程序用户提供进行应用程序内购买的机会。在布局文件中,我们将为用户提供以不同面额进行购买的机会。
InAppPurchaseActivity.java
注意: getAllUserPurchase() 和 itemPurchaseAvailability() 方法应在非 UI 线程中调用,以避免应用程序崩溃。
创建 Helper 包目录
创建一个新的包文件夹并将其命名为 helpers。在包内,创建一个新的 java 文件 Helper.java。
Helper.java
测试应用内结算购买
您在应用内购买测试期间可能会遇到错误,
您请求的商品无法购买
解决方案 - 根据 Stackoverflow 的 AndreiBogdan 的说法,
Android 开发者博客还推荐了有关销售应用内产品的培训课程。要查看完整的实现并了解如何测试应用程序,请查看本教程:销售应用内产品
There is a full example of Android In-App Billing v3 step by step is given here with screenshot. Please check the tutorial:
Android In-App Billing v3 using ServiceConnection Class
Hope it will help.
For more clarification, go through this tutorial: Implementing In-app Billing in Version 3 API
Steps to follow to Integrate In-app Billing library in our project
Update your AndroidManifest.xml file.
Create a ServiceConnection and bind it to IInAppBillingService.
Send In-app Billing requests from your application to IInAppBillingService.
Handle In-app Billing responses from Google Play.
Update AndroidManifest.xml
Add the permissions in Manifest.xml file
Adding the AIDL file to your project
Build your application. You should see a generated file named IInAppBillingService.java in the /gen directory of your project.
Update Dependencies in build.gradle file
InAppPurchaseActivity.java and activity_in_app_purchase.xml
This is where will offer our app users the opportunity to to make in-app purchase. In the layout file, we will give the user the opportunity to make purchase in different denominations.
InAppPurchaseActivity.java
Note: getAllUserPurchase() and itemPurchaseAvailability() methods should be called in non UI Thread to avoid app crashing.
Create Helper Package Directory
Create a new package folder and name it helpers. Inside the package, create a new java file Helper.java.
Helper.java
Testing In-App Billing Purchase
Errors You might encounter during In-App purchase testing
the item you requested is not available for purchase
Solution – According to AndreiBogdan in Stackoverflow,
Android Developer Blog also recommends a training class on Selling In-app Products. To see a complete implementation and learn how to test the application, Please check this tutorial: Selling In-app Products
好吧,这是网上没有太多可用文档的事情之一,所以我将尽力一步一步解释所有内容。取自我的博客文章,这是一个更详细的版本(带有屏幕截图),在 Millibit 上。言归正传,
第一步:权限
这是最简单的一步。导航到您的manifest.xml 文件并在您的标签下添加以下行:
这将为您的应用授予访问应用内结算的权限。如果您的目标版本高于 API 22,则需要确保在运行时授予此权限。
第二步:Play 管理中心
现在您需要将应用程序上传到 Google Play 管理中心。我们还没有向公众发布我们的应用程序(不用担心),我们只是将其上传到 BETA RELEASE 部分,这将使我们能够测试应用内购买。我们需要这样做的原因是 Google 需要上传您的 APK 的某个版本才能使计费流程真正发挥作用。
转至https://play.google.com/apps/publish/< /p>
按照设置应用程序的步骤
转到应用程序版本
导航到测试版
创建在 Android studio 中创建应用的 APK,并将其上传到 Play Console 中的 Beta 版本
(在发布之前,请确保您已经填写了商店列表,内容分级、定价和分发)
第三步:设置项目
好的,这是您必须复制并粘贴一堆文件的部分。
首先,获取 这个 文件,下载它,并将其放在
src/main
下,它应该将自己构建到一个文件夹中接下来,获取 整个 util 文件夹并将其粘贴到
src/java 文件夹中。
然后重建您的项目以解决错误。Util 文件夹包含以下类:
第四步: 创建产品
创建托管产品
单击保存并创建“定价模板”
在这里,您将选择该产品的价格。您可以选择不同国家/地区的价格,或者如果您只选择价格下的所有国家/地区,则价格会自动调整:
最后,记下您的产品 ID。我们将在接下来的几个步骤中使用此 ID。
前往“服务和服务” API”并获取您的 Base64EncodedString。将其复制并粘贴到记事本中的某个位置,以便您可以访问它。不要与任何人分享这个,他们将能够用它来做恶意的事情。
第五步:终于!我们可以开始编码:
我们首先会绑定应用内计费库,并查询用户已购买/未购买的商品。然后,我们将购买我们之前设置的产品。
首先,导入我们之前设置的所有内容:
现在我们将使用一个名为 mHelper 的 IabHelper 对象,我们将用它来做所有事情。
好吧,让我来分析一下这里发生了什么。基本上,我们调用“startSetup”来初始化我们的“IabHelper”。如果设置成功,我们会查询用户已经购买了哪些商品,并将响应存储在
mGotInventoryListener
中,我们接下来将对其进行编码:上面的代码非常不言自明。基本上,它只是检查用户已经购买了什么。现在我们知道用户是否已经购买了我们的产品,我们就知道是否要求他们购买我们的产品!如果他们以前从未购买过我们的产品,让我们开始一个购买请求:
这里我们使用以下内容购买商品(使用我们之前在游戏控制台中生成的 ID):
请注意,我们将
mPurchaseFinishedListener
传递给了参数。这意味着购买的结果将返回给这个监听器。然后,我们只需检查购买是否为空,如果不为空,则奖励用户他们购买的任何功能。别让听众泄密!当应用程序销毁时,我们必须销毁它们。
最后,如果您想使用您购买的产品并使其可供再次购买,您可以轻松做到这一点。一个例子是,如果用户为虚拟汽车购买了汽油,但汽油用完了。他们需要再次购买相同的产品,您可以通过消费将其用于第二次购买:
就是这样!您现在可以开始赚钱了。真的就是这么简单!
再次强调,如果您想要本教程的更详细版本(包含屏幕截图和图片),请访问 原始帖子在这里。如果您还有其他问题,请在评论中告诉我。
Okay this is one of those things that doesn't have very much documentation available online, so I'm going to do my best to explain everything step by step. Taken from my blog post, which is a more detailed version of this (with screenshots), here on The Millibit. Without further ado,
Step One: Permissions
This is the easiest step. Navigate to your manifest.xml file and add the following line under your tag:
This will give your app the permissions to access In-App Billing. If you are targetting versions above API 22, you will need to make sure that this permission is granted at runtime.
Step Two: Play Console
Now you need to upload your app to the Google Play Console. We are not publishing our app to the public yet (don’t worry), we are just uploading it to the BETA RELEASE section, which will allow us to test In-App Purchases. The reason we need to do this is that Google needs to have some version of your APK uploaded for the billing processes to actually work.
Go to https://play.google.com/apps/publish/
Create the Application
Follow the steps to set up your app
Go to App Releases
Navigate to Beta
Create an APK of your app in Android studio and upload it to the Beta production in the Play Console
(before releasing make sure that you have already filled out the Store Listing ,Content Rating and Pricing and Distribution)
Step Three: Setup Project
Okay this is the part where you have to copy and paste a bunch of files.
First, grab this file, download it, and place it under
src/main
It should build itself into a folderNext, grab this entire util folder and paste it into
src/java folder.
Then rebuild your project to resolve errors.The Util Folder Contains The Following Classes:
Step Four: Create Products
Create Managed Product
Click save and make a “pricing template”
Here, you will select the price of this product. You can choose the price for different countries, or have it automatically adjust if you just select all countries under your price:
Finally, note the ID of your product. We will use this ID in the next few steps.
Head over to “Services & APIs” and grab your Base64EncodedString. Copy and paste this to a notepad somewhere so that you have access to it. Do not share this with anyone, they will be able to do malicious things with it.
Step Five: Finally! We can start coding:
We will first bind to the in-app billing library, and query for what the user has/hasn’t bought. Then, we will buy the product that we set up earlier.
First, import everything we set up earlier:
Now we will use an IabHelper object called mHelper, and we will do everything with this.
Okay, let me break down what’s going on here. Basically, we are calling “startSetup” to initialize our “IabHelper”. If the setup is successful, we query what purchases the user already has and store the responses in
mGotInventoryListener
, which we will code next:The above code is pretty self-explanatory. Basically, it just checks what purchases the user already has. Now that we know whether or not the user has already purchased our product, we know whether or not to ask them to purchase our item! If they’ve never bought our product before, let’s start a purchase request:
Here we purchase the item (with the ID we generated in the play console earlier) with the following:
Notice that we passed
mPurchaseFinishedListener
into the parameters. This means that the result of the purchase will be returned to this listener. Then, we simply check if the purchase is null, and if not, award the user with whatever feature they bought.Don’t let the listeners leak! We must destroy them when the app destroys.
Finally, if you’d like to consume your purchase, making it available for purchase again, you can do so easily. An example of this is if a user bought gas for a virtual car, and it ran out. They need to purchase the same product again, and you can make it available for a second purchase by consuming it:
That’s it! You can now start making money. It’s really that simple!
Again, if you want a more detailed version of this tutorial, with screenshots and pictures, visit the original post here. Let me know in the comments if you have any more questions.
为了更好地了解如何使用 google play billing 库进行应用内结算,请参阅下面的流程图:
您可以按照我在本文中解释的逐步集成进行操作:
https://medium.com/@surabhichoudhary/in -app-purchasing-with-google-play-billing-library-6a72e289a78e
如果您需要演示关于这一点,这是项目链接:https://github.com/surabhi6/InAppPurchaseDemo
For better understanding of how in-app billing works using google play billing library, refer to the flow chart below:
You can follow the integration step by step that I have explained in this article :
https://medium.com/@surabhichoudhary/in-app-purchasing-with-google-play-billing-library-6a72e289a78e
If you need demo on this, this is the project link : https://github.com/surabhi6/InAppPurchaseDemo
如果您想使用一个简单的库在 Google Play 和 Amazon Appstore 上发布,您可以使用 RoboBillingLibrary。它将两者的细节抽象为一个易于使用的库。详细说明位于 Github 页面上。
If you want to use an easy library to publish across Google Play and the Amazon Appstore, you could go with RoboBillingLibrary. It abstracts the details of both into one easy to use library. Detailed instructions are on the Github page.
我开发了 Android 应用内计费库,它使用“com.android.billingclient:billing:2.1.0”
以下是它的属性:
库源
I have developed Android In app billing library which uses "com.android.billingclient:billing:2.1.0"
Here are its properties:
library source
基本上你需要在两个地方购买代码
首先在你的 MainActivity 中
您的应用程序每次都会打开,因此请检查那里的购买状态。需要实施购买更新的监听器。
其次,当单击按钮开始购买时。因此,您的按钮所在的位置其活动需要
实施购买更新的监听器。
如果您的按钮位于 MainActivity 下,那么您只需要在一个地方(即 MainActivity)购买代码。
有关更多信息,您可以在此处关注我的工作教程:
https://programtown.com/how-to-make-multiple-in-app-purchase-in-android-using-google-play-billing-library/
Basically you need purchase code at two places
Firstly at your MainActivity where
your app opens every time, so check purchase statuses there. Need to implement purchasesupdated listener.
Secondly when button is clicked to initiate purchase. So where your button reside its activity need to
implement purchasesupdated listener.
If your button reside under MainActivity then you just need purchase code at one place i.e MainActivity.
For more you can follow my working tutorial here:
https://programtown.com/how-to-make-multiple-in-app-purchase-in-android-using-google-play-billing-library/