(1)Preparing Your In-app Billing Application
http://developer.android.com/training/in-app-billing/preparing-iab-app.html#AddLibrary
Before you can start using the In-app Billing service, you'll need to add the library that contains the In-app Billing Version 3 API to your Android project. You also need to set the permissions for your application to communicate with Google Play. In addition, you'll need to establish a connection between your application and Google Play. You should also verify that the In-app Billing API version that you are using in your application is supported by Google Play.
Download the Sample Application
In this training class, you will use a reference implementation for the In-app Billing Version 3 API called the TrivialDrive
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.
To download the sample application:
- Open the Android SDK Manager.
- In the SDK Manager, expand the
Extras
section. - Select Google Play Billing Library.
- Click Install packages to complete the download.
The sample files will be installed to <sdk>/extras/google/play_billing/
.
Add Your Application to the Developer Console
The Google Play Developer Console is where you publish your In-app Billing application and manage the various digital goods that are available for purchase from your application. When you create a new application entry in the Developer Console, it automatically generates a public license key for your application. You will need this key to establish a trusted connection from your application to the Google Play servers. You only need to generate this key once per application, and don’t need to repeat these steps when you update the APK file for your application.
To add your application to the Developer Console:
- Go to the Google Play Developer Console site and log in. You will need to register for a new developer account, if you have not registered previously. To sell in-app items, you also need to have a Google Wallet merchant account.
- Click on Try the new design to access the preview version of the Developer Console, if you are not already logged on to that version.
- In the All Applications tab, add a new application entry.
- Click Add new application.
- Enter a name for your new In-app Billing application.
- Click Prepare Store Listing.
- In the Services & APIs tab, find and make a note of the public license key that Google Play generated for your application. This is a Base64 string that you will need to include in your application code later.
Your application should now appear in the list of applications in Developer Console.
Add the In-app Billing Library
To use the In-app Billing Version 3 features, you must add the IInAppBillingService.aidl
file to your Android project. This Android Interface Definition Language (AIDL) file defines the interface to the Google Play service.
You can find the IInAppBillingService.aidl
file in the provided sample app. Depending on whether you are creating a new application or modifying an existing application, follow the instructions below to add the In-app Billing Library to your project.
New Project
To add the In-app Billing Version 3 library to your new In-app Billing project:
- Copy the
TrivialDrive
sample files into your Android project. - Modify the package name in the files you copied to use the package name for your project. In Eclipse, you can use this shortcut: right-click the package name, then select Refactor > Rename.
- Open the
AndroidManifest.xml
file and update the package attribute value to use the package name for your project. - Fix import statements as needed so that your project compiles correctly. In Eclipse, you can use this shortcut: press Ctrl+Shift+O in each file showing errors.
- Modify the sample to create your own application. Remember to copy the Base64 public license key for your application from the Developer Console over to your
MainActivity.java
.
Existing Project
To add the In-app Billing Version 3 library to your existing In-app Billing project:
- Copy the
IInAppBillingService.aidl
file to your Android project.- In Android Studio: Create a directory named
aidl
undersrc/main
, add a new packagecom.android.vending.billing
in this directory, and import theIInAppBillingService.aidl
file into this package. - In Eclipse: Import the
IInAppBillingService.aidl
file into your/src
directory. - In other dev environments: Create the following directory
/src/com/android/vending/billing
and copy theIInAppBillingService.aidl
file into this directory.
- In Android Studio: Create a directory named
- Build your application. You should see a generated file named
IInAppBillingService.java
in the/gen
directory of your project. - Add the helper classes from the
/util
directory of theTrivialDrive
sample to your project. Remember to change the package name declarations in those files accordingly so that your project compiles correctly.
Your project should now contain the In-app Billing Version 3 library.
Set the Billing Permission
Your app needs to have permission to communicate request and response messages to the Google Play’s billing service. To give your app the necessary permission, add this line in your AndroidManifest.xml
manifest file:
<uses-permissionandroid:name="com.android.vending.BILLING"/>
nitiate a Connection with Google Play
You must bind your Activity to Google Play’s In-app Billing service to send In-app Billing requests to Google Play from your application. 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.
To set up synchronous communication with Google Play, create an IabHelper
instance in your activity's onCreate
method. In the constructor, pass in the Context
for the activity, along with a string containing the public license key that was generated earlier by the Google Play Developer Console.
Security Recommendation: It is highly recommended that you do not hard-code the exact public license key string value as provided by Google Play. 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. This approach makes it more difficult for malicious third-parties to modify the public license key string in your APK file.
IabHelper mHelper;@Overridepublicvoid onCreate(Bundle savedInstanceState){ // ... String base64EncodedPublicKey; // compute your public key and store it in base64EncodedPublicKey mHelper =newIabHelper(this, base64EncodedPublicKey);}
Next, perform the service binding by calling the startSetup
method on the IabHelper
instance that you created. Pass the method an OnIabSetupFinishedListener
instance, which is called once the IabHelper
completes the asynchronous setup operation. As part of the setup process, the IabHelper
also checks if the In-app Billing Version 3 API is supported by Google Play. If the API version is not supported, or if an error occured while establishing the service binding, the listener is notified and passed an IabResult
object with the error message.
mHelper.startSetup(newIabHelper.OnIabSetupFinishedListener(){ publicvoid 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! }});
If the setup completed successfully, you can now use the mHelper
reference to communicate with the Google Play service. When your application is launched, it is a good practice to query Google Play to find out what in-app items are owned by a user. This is covered further in the Query Purchased Items section.
Important: Remember to unbind from the In-app Billing service when you are done with your activity. If you don’t unbind, the open service connection could cause your device’s performance to degrade. To unbind and free your system resources, call the IabHelper
's dispose
method when your Activity
is destroyed.
@Overridepublicvoid onDestroy(){ super.onDestroy(); if(mHelper !=null) mHelper.dispose(); mHelper =null;}
相关推荐
Expo-2022-Cleaning-and-Preparing-Time-Series-Data-main
The-Ultimate-Strategy-to-Preparing-for-a-Coding-Interview-Medium.pdf
"EBS12-Preparing For Your Installation-EDU41CFY.pdf"文档提供了关于如何有效地进行安装准备的指导。 首先,Rapid Install工具是Oracle EBS12安装过程中的核心组件。它旨在简化和自动化安装流程,减少安装时间,...
108. Preparing an iOS 10 Application for In-App Purchases 109. An iOS 10 In-App Purchase Tutorial 110. Configuring and Creating App Store Hosted Content for iOS 10 In-App Purchases 111. Preparing and ...
Preparing an iOS 8 Application for In-App Purchases Chapter 97. An iOS 8 In-App Purchase Tutorial Chapter 98. Configuring and Creating App Store Hosted Content for iOS 8 In-App Purchases Chapter 99. ...
Preparing an iOS 9 Application for In-App Purchases Chapter 103. An iOS 9 In-App Purchase Tutorial Chapter 104. Configuring and Creating App Store Hosted Content for iOS 9 In-App Purchases Chapter ...
【标题】"Preparing-For-Toeic-Test:学习词汇,语法,听力,阅读,练习,youtube视频"指的是一个专门针对托福国际英语测验(TOEIC)的备考资源包。这个资源旨在帮助用户全面提高英语能力,涵盖词汇、语法、听力、...
EIA-JEDEC-JEP134:1998 Guidelines for Preparing Customer-Supplied Background Information Relating to a Semiconductor-Device Failure Analysis - 完整英文电子版(16页).zip
Chapter 1 : AIR Installing the Adobe Development Tools Installing the AIR Runtime on an Android Device What Is in the AIR SDK New ActionScript Libraries AIR on the Desktop Versus AIR on Android Mobile...
Android上的Scala:准备环境 这是一个可在Android上与Scala一起使用的模板项目 该项目的结构类似于典型的android项目。 AndroidManifest: src/main Scala源文件: src/main/scala Android资源文件: src/main/...
【准备迎接JDK 9:Oracle Java平台的未来】 JDK 9是Java开发工具包的一个重大更新,它标志着Java平台的进一步发展和创新。在2016年的QCon上海会议上,Rory O'Donnell(Oracle公司的Java平台组成员)分享了关于JDK 9...
根据给定文件信息,本文主要阐述了以萘为原料,通过气相催化氧化法制备1,4-萘醌的研究,其中V2O5作为催化剂的主要活性组分。研究中确定了催化剂的活性组分、助催化剂、制备方法以及最佳反应温度,并探讨了复合催化剂...
为AI开发做准备 AI开发之前的准备过程 记录开发Al的必要准备步骤的项目。 目标 从Tensorflow 2.0和...1.人工智能的概念和准备 2. Tensorflow 2.0和Pytorch 3.深度学习基础 4.深度学习的最新趋势 5.深度学习实践项目
In this book, the following issues are taken into account: - Recommendations with regard to preparation prior to application - Visiting the institution post shortlisting, the makeup of the interview ...
1. Oracle Database 10g Release 2 (10.2.0.2):这是EBS 12的基础,提供了数据存储和管理功能。安装时需要选择适当的字符集以支持所选语言。 2. Oracle Application Server 10.1.2:该版本的应用服务器主要提供...