google官方文档地址 http://developer.android.com/google/play/billing/billing_integrate.html
不过,在国内无法直接使用,必须用国外VPN才行。
Implementing In-app Billing
In-app Billing on Google Play provides a straightforward, simple interface for sending In-app Billing requests and managing In-app Billing transactions using Google Play. The information below covers the basics of how to make calls from your application to the In-app Billing service using the Version 3 API.
Note: To see a complete implementation and learn how to test your application, see the Selling In-app Products training class. The training class provides a complete sample In-app Billing application, including convenience classes to handle key tasks related to setting up your connection, sending billing requests and processing responses from Google Play, and managing background threading so that you can make In-app Billing calls from your main activity.
Before you start, be sure that you read the In-app Billing Overview to familiarize yourself with concepts that will make it easier for you to implement In-app Billing.
To implement In-app Billing in your application, you need to do the following:
- Add the In-app Billing library to your project.
- Update your
AndroidManifest.xml
file. - Create a
ServiceConnection
and bind it toIInAppBillingService
. - Send In-app Billing requests from your application to
IInAppBillingService
. - Handle In-app Billing responses from Google Play.
Adding the AIDL file to your project
IInAppBillingService.aidl
is an Android Interface Definition Language (AIDL) file that defines the interface to the In-app Billing Version 3 service. You will use this interface to make billing requests by invoking IPC method calls.
To get the AIDL file:
- 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 IInAppBillingService.aidl
file will be installed to <sdk>/extras/google/play_billing/
.
To add the AIDL to your project:
- Copy the
IInAppBillingService.aidl
file to your Android project.- If you are using Eclipse:
- If you are starting from an existing Android project, open the project in Eclipse. If you are creating a new Android project from scratch, click File > New > Android Application Project, then follow the instructions in the New Android Application wizard to create a new project in your workspace.
- In the
/src
directory, click File > New > Package, then create a package namedcom.android.vending.billing
. - Copy the
IInAppBillingService.aidl
file from<sdk>/extras/google/play_billing/
and paste it into thesrc/com.android.vending.billing/
folder in your workspace.
- If you are developing in a non-Eclipse environment: Create the following directory
/src/com/android/vending/billing
and copy theIInAppBillingService.aidl
file into this directory. Put the AIDL file into your project and use the Ant tool to build your project so that theIInAppBillingService.java
file gets generated.
- If you are using Eclipse:
- Build your application. You should see a generated file named
IInAppBillingService.java
in the/gen
directory of your project.
Updating Your Application's Manifest
In-app billing relies on the Google Play application, which handles all communication between your application and the Google Play server. To use the Google Play application, your application must request the proper permission. You can do this by adding the com.android.vending.BILLING
permission to your AndroidManifest.xml file. If your application does not declare the In-app Billing permission, but attempts to send billing requests, Google Play will refuse the requests and respond with an error.
To give your app the necessary permission, add this line in your Android.xml
manifest file:
<uses-permissionandroid:name="com.android.vending.BILLING"/>
Creating a ServiceConnection
Your application must have a ServiceConnection
to facilitate messaging between your application and Google Play. At a minimum, your application must do the following:
- Bind to
IInAppBillingService
. - Send billing requests (as IPC method calls) to the Google Play application.
- Handle the synchronous response messages that are returned with each billing request.
Binding to IInAppBillingService
To establish a connection with the In-app Billing service on Google Play, implement a ServiceConnection
to bind your activity to IInAppBillingService
. Override the onServiceDisconnected
and onServiceConnected
methods to get a reference to the IInAppBillingService
instance after a connection has been established.
IInAppBillingService mService; ServiceConnection mServiceConn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { mService = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { mService = IInAppBillingService.Stub.asInterface(service); } };
In your activity’s onCreate
method, perform the binding by calling the bindService
method. Pass the method an Intent
that references the In-app Billing service and an instance of the ServiceConnection
that you created, and explicitly set the Intent's target package name to com.android.vending
— the package name of Google Play app.
Caution: To protect the security of billing transactions, always make sure to explicitly set the intent's target package name to com.android.vending
, using setPackage()
as shown in the example below. Setting the package name explicitly ensures that only the Google Play app can handle billing requests from your app, preventing other apps from intercepting those requests.
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND"); serviceIntent.setPackage("com.android.vending"); bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
You can now use the mService reference to communicate with the Google Play service.
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. This example shows how to perform the unbind operation on a service connection to In-app Billing called mServiceConn
by overriding the activity’s onDestroy
method.
@Override public void onDestroy() { super.onDestroy(); if (mService != null) { unbindService(mServiceConn); } }
For a complete implementation of a service connection that binds to the IInAppBillingService
, see the Selling In-app Products training class and associated sample.
Making In-app Billing Requests
Once your application is connected to Google Play, you can initiate purchase requests for in-app products. Google Play provides a checkout interface for users to enter their payment method, so your application does not need to handle payment transactions directly. When an item is purchased, Google Play recognizes that the user has ownership of that item and prevents the user from purchasing another item with the same product ID until it is consumed. You can control how the item is consumed in your application, and notify Google Play to make the item available for purchase again. You can also query Google Play to quickly retrieve the list of purchases that were made by the user. This is useful, for example, when you want to restore the user's purchases when your user launches your app.
Querying for Items Available for Purchase
In your application, you can query the item details from Google Play using the In-app Billing Version 3 API. To pass a request to the In-app Billing service, first create a Bundle
that contains a String ArrayList
of product IDs with key "ITEM_ID_LIST", where each string is a product ID for an purchasable item.
ArrayList<String> skuList = new ArrayList<String> (); skuList.add("premiumUpgrade"); skuList.add("gas"); Bundle querySkus = new Bundle(); querySkus.putStringArrayList(“ITEM_ID_LIST”, skuList);
To retrieve this information from Google Play, call the getSkuDetails
method on the In-app Billing Version 3 API, and pass the method the In-app Billing API version (“3”), the package name of your calling app, the purchase type (“inapp”), and the Bundle
that you created.
Bundle skuDetails = mService.getSkuDetails(3, getPackageName(), "inapp", querySkus);
If the request is successful, the returned Bundle
has a response code of BILLING_RESPONSE_RESULT_OK
(0).
Warning: Do not call the getSkuDetails
method on the main thread. Calling this method triggers a network request which could block your main thread. Instead, create a separate thread and call the getSkuDetails
method from inside that thread.
相关推荐
【cordova-markets-iab: 科尔多瓦所有市场IAB】是一款专为Apache Cordova框架设计的插件,旨在实现跨多个应用市场(如Google Play、Apple App Store等)的应用内购买功能。Cordova是一种流行的开源框架,它允许...
总的来说,"skubit-iab"库是Android开发者实现In-App Billing功能的有力助手,它通过封装复杂的Google Play服务API,让开发者能够更专注于应用的核心功能,而不是繁琐的支付逻辑。通过理解和熟练运用这个库,可以...
Google Play 是全球最大的 Android 应用市场,为开发者提供了丰富的功能,其中之一就是应用内支付(In-App Billing, IAB)服务。IInAppBillingService.zip 文件正是与这个服务相关的,其中包含了 ...
Google Play应用内结算(In-App Billing, IAB)服务是Google Play生态系统的一部分,为开发者提供了便捷的支付处理方式。 **版本3**的IAB引入了更安全、更稳定的功能,优化了购买流程,提高了用户体验。此版本可能...
Google Play 提供了一个官方的 IAB API,但它的使用并不总是直观,尤其是对于开发者来说,进行调试和测试时可能会遇到挑战。 BillingX 库可能提供了以下特性: 1. **调试支持**:为了帮助开发者在开发和测试阶段更...
移动支付方面,Apple Pay、Google Wallet和Square等移动支付工具广泛使用,随着无现金交易的普及,移动支付市场持续扩大。 【美国电商市场现状分析】 美国电商市场发展成熟,亚马逊、eBay和沃尔玛等大型电商平台...
这个插件主要基于Java语言编写,因为Android系统的核心API是用Java设计的,而Google Play服务的IAB接口也通常需要通过Java来调用。Java是Android开发的标准语言,具有广泛的社区支持和丰富的库资源,使得开发这样的...
此过程中,用户将被引导至Google Play支付界面,完成支付后返回应用。开发者只需调用相应API即可实现这一过程。 3. **消费验证**:应用内购买的商品分为消耗性和非消耗性两种。消耗品可以被购买多次,而非消耗品...
横幅广告的尺寸和格式通常遵循IAB(Interactive Advertising Bureau)的标准。 9. **富媒体广告**:富媒体广告包括视频、游戏、扩展面板等多种形式,它们提供丰富的交互体验。这些广告可能需要Flash(已逐渐被淘汰...
1. **Google Play Store注册**:首先,你需要在Google Play Console创建一个开发者账户,这需要支付一次性的注册费用,并提供必要的个人信息和公司信息。 2. **应用准备**:确保你的应用程序已经完成了所有功能的...
在Android应用开发中,内购(In-App Billing,IAB)是一项重要的功能,它允许开发者在应用内部向用户售卖虚拟商品或服务。OpenIAB(Open In-App Billing)是一个开源项目,它为Android应用提供了与Google Play Store...
- **流量和市场热度**:根据Google Trends和SimilarWeb的数据,Quip在2017年12月的搜索热度开始领跑,超越其他传统品牌,网站流量超过百万人次。 #### BarkBox品牌案例分析 BarkBox则针对宠物主人,特别是那些喜欢...