在开始使用 In-app Billing 服务以前,你须要先把包含 In-app Billing Version 3 API 的库添加到你的Android工程中。你还须要设置你的应用和Google Play通讯须要的权限。另外,你还须要在你的应用和Google Play之间创建一个稳定链接。最后还要确认Google Play支持你应用程序使用的In-app Billing API版本html
在这个训练课程,你将用到一个叫作 TrivialDrive
的示例程序对于In-app Billing Version 3的参考实现。 这个例子里有些便捷类能够快捷设置In-app Billing服务,编码解码数据类型以及在你程序的主线程处理In-app Billing请求。注1java
去下载例子程序:android
Extras
部分.这个例子文件将会安装到 <sdk>/extras/google/play_billing/
. (译者注:就是在你电脑上SDK目录下面)windows
在 Google Play 开发者控制台,你能够发布你的添加了 In-app Billing 的应用,还能够管理来自你的应用中能够销售的各类数字商品。当你在开发者控制台建立一个新应用后,控制台会为你的应用自动生成一个公钥(public license key)。你将须要使用这个公钥在你的应用和Google Play服务器之间创建一个可信赖的链接。这个公钥在应用建立的时候生成,在之后更新你程序的APK文件时没必要再次生成。安全
把你的应用添加到开发者控制台:服务器
你的应用如今应该出如今开发者控制台的应用列表中。网络
为了使用 In-app Billing Version 3 的功能,你必须把 IInAppBillingService.aidl
文件添加到你的工程中。 这个 Android Interface Definition Language (AIDL) 文件定义了Google Play 服务可用的接口。app
你能够在提供的例子程序里找到这个IInAppBillingService.aidl文件。根据你是建立一个新的应用仍是修改现有的应用,跟随下面的指导把In-app Billing库添加到你的工程中。
eclipse
添加 In-app Billing Version 3 library 到你的新 In-app Billing 工程:异步
TrivialDrive
例子程序文件复制到你的工程。AndroidManifest.xml
文件,把包名属性值改为你工程的包名。MainActivity.java
.添加 In-app Billing Version 3 library 到你已有的 In-app Billing 工程:
把 IInAppBillingService.aidl文件复制到你的
Android 工程。
IInAppBillingService.aidl
文件引入到工程的 /src
目录下。/src/com/android/vending/billing
并把 IInAppBillingService.aidl
文件复制到这个目录下。/gen目录下应该能够看到一个生成的文件IInAppBillingService.java
TrivialDrive
例子中 /util 目录下的辅助类添加到你的工程。记得改变这些文件中声明的包名,这样你的工程才能够成功编译。如今你的工程应该已经包含了 In-app Billing Version 3 library.
你的程序应该有个能够向 Google Play 的购买服务发送请求以及收到回应的许可。为了给你的应用添加必要的许可,把下面这行许可内容添加到你的manifest文件AndroidManifest.xml:
<uses-permission android:name="com.android.vending.BILLING" />
为了向In-app Billing 发送请求和收到回应,你必须把你的Activity绑定到 Google Play 的In-app Billing服务。在例子中已经提供了处理绑定服务的便捷类,因此你没必要直接管理网络链接。
为了和Google Play设置同步通讯,在你程序Activity的 onCreate 方法中建立一个 IabHelper
实例。在 IabHelper 构造方法中传Activity的 Context
还有先前在开发者控制台生成的公钥字符串。
安全建议: 强烈建议你不要把公钥原样的编写到代码里。可替代的,在把公钥传给构造方法前,你能够动态的用子字符串构造完整的公钥,也能够从一个加密的存储中获取它。注3 这样作能够有效地防止有些居心不良的第三方修改你APK文件中的公钥。
IabHelper mHelper; @Override public void onCreate(Bundle savedInstanceState) { // ... String base64EncodedPublicKey; // compute your public key and store it in base64EncodedPublicKey mHelper = new IabHelper(this, base64EncodedPublicKey); }
接下来,调用你建立的 IabHelper实例中的
startSetup
方法来执行服务绑定。在方法中传一个OnIabSetupFinishedListener
,这个listener会在 IabHelper
完成异步设置后调用一次。在设置过程当中 IabHelper
也会检测Google Play是否支持In-app Version 3 API,若是这个版本的API不被支持,或者在创建服务绑定时出现错误,这个listener就会被通知而且传给一个带有错误消息的IabResult对象。
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { public void onIabSetupFinished(IabResult result) { if (!result.isSuccess()) { // Oh noes, there was a problem. Log.d(TAG, "Problem setting up In-app Billing: " + result); } // Hooray, IAB is fully set up! } });
若是这个设置成功完成,你如今就可使用这个 mHelper
引用来和Google Play服务通讯了。当你的应用启动时,最好去查询下Google Play,看看用户都拥有哪些内购物品。详细内容在后来的 Query Purchased Items 部分。
重要: 记得在你用完activity后解除和In-app Billing服务之间的绑定。若是你不解除绑定,这个开放的服务链接可能影响你设备的性能。为了解除绑定并释放你的系统资源,能够在你的 Activity 销毁的时候调用 IabHelper的
dispose 方法。
@Override public void onDestroy() { super.onDestroy(); if (mHelper != null) mHelper.dispose(); mHelper = null; }
注1:原文 In this training class, you will use a reference implementation for the In-app Billing Version 3 API called the sample application. The sample includes convenience classes to quickly set up the In-app Billing service, marshal and unmarshal data types, and handle In-app Billing requests from the main thread of your application.注2:原文 The convenience classes provided in the sample handles the binding to the In-app Billing service, so you don’t have to manage the network connection directly.注3:原文 Instead, you can construct the whole public license key string at runtime from substrings, or retrieve it from an encrypted store, before passing it to the constructor.TrivialDrive