public abstract class BillingClient extends Object
It provides convenience methods for in-app billing. You can create one instance of this class for your application and use it to process in-app billing operations. It provides synchronous (blocking) and asynchronous (non-blocking) methods for many common in-app billing operations.
All methods are supposed to be called from the Ui thread and all the asynchronous callbacks will be returned on the Ui thread as well.
After instantiating, you must perform setup in order to start using the object. To perform
setup, call the startConnection(com.android.billingclient.api.BillingClientStateListener) method and provide a listener; that listener will be
notified when setup is complete, after which (and not before) you may start calling other
methods. After setup is complete, you will typically want to request an inventory of owned items
and subscriptions. See queryPurchases(java.lang.String) and querySkuDetailsAsync(com.android.billingclient.api.SkuDetailsParams, com.android.billingclient.api.SkuDetailsResponseListener).
When you are done with this object, don't forget to call endConnection() to ensure
proper cleanup. This object holds a binding to the in-app billing service and the manager to
handle broadcast events, which will leak unless you dispose it correctly. If you created the
object inside the Activity.onCreate(android.os.Bundle) method, then the recommended place to dispose is the
the Activity.onDestroy() method.
To get library logs inside Android logcat, set corresponding logging level.
E.g.: adb shell setprop log.tag.BillingClient VERBOSE
| Modifier and Type | Class and Description |
|---|---|
static interface |
BillingClient.BillingResponse
Possible response codes.
|
static class |
BillingClient.Builder
Builder to configure and create a BillingClient instance.
|
static interface |
BillingClient.ChildDirected
Developers are able to specify whether you would like your app to be treated as child-directed
for purposes of the Children’s Online Privacy Protection Act (COPPA) -
http://business.ftc.gov/privacy-and-security/childrens-privacy.
|
static interface |
BillingClient.FeatureType
Features/capabilities supported by
isFeatureSupported(String). |
static interface |
BillingClient.SkuType
Supported SKU types.
|
static interface |
BillingClient.UnderAgeOfConsent
Developers are able to specify whether to mark your ad requests to receive treatment for
users in the European Economic Area (EEA) under the age of consent.
|
| Constructor and Description |
|---|
BillingClient() |
| Modifier and Type | Method and Description |
|---|---|
abstract void |
consumeAsync(String purchaseToken,
ConsumeResponseListener listener)
Consumes a given in-app product.
|
abstract void |
endConnection()
Close the connection and release all held resources such as service connections.
|
abstract int |
isFeatureSupported(String feature)
Check if specified feature or capability is supported by the Play Store.
|
abstract boolean |
isReady()
Checks if the client is currently connected to the service, so that requests to other methods
will succeed.
|
abstract int |
launchBillingFlow(Activity activity,
BillingFlowParams params)
Initiate the billing flow for an in-app purchase or subscription.
|
abstract void |
launchPriceChangeConfirmationFlow(Activity activity,
PriceChangeFlowParams params,
PriceChangeConfirmationListener listener)
Initiate a flow to confirm the change of price for an item subscribed by the user.
|
abstract void |
loadRewardedSku(RewardLoadParams params,
RewardResponseListener listener)
Loads a rewarded sku in the background and returns the result asynchronously.
|
static BillingClient.Builder |
newBuilder(Context context)
Constructs a new
BillingClient.Builder instance. |
abstract void |
queryPurchaseHistoryAsync(String skuType,
PurchaseHistoryResponseListener listener)
Returns the most recent purchase made by the user for each SKU, even if that purchase is
expired, canceled, or consumed.
|
abstract Purchase.PurchasesResult |
queryPurchases(String skuType)
Get purchases details for all the items bought within your app.
|
abstract void |
querySkuDetailsAsync(SkuDetailsParams params,
SkuDetailsResponseListener listener)
Perform a network query to get SKU details and return the result asynchronously.
|
abstract void |
startConnection(BillingClientStateListener listener)
Starts up BillingClient setup process asynchronously.
|
@UiThread public static BillingClient.Builder newBuilder(@NonNull Context context)
BillingClient.Builder instance.context - It will be used to get an application context to bind to the in-app billing
service.@UiThread public abstract int isFeatureSupported(String feature)
feature - One of BillingClient.FeatureType constants.@UiThread public abstract boolean isReady()
Returns true if the client is currently connected to the service, false otherwise.
Note: It also means that INAPP items are supported for purchasing, queries and all other
actions. If you need to check support for SUBSCRIPTIONS or something different, use isFeatureSupported(String) method.
@UiThread public abstract void startConnection(@NonNull BillingClientStateListener listener)
BillingClientStateListener listener when the setup process is complete.listener - The listener to notify when the setup process is complete.@UiThread public abstract void endConnection()
Call this method once you are done with this BillingClient reference.
@UiThread public abstract int launchBillingFlow(Activity activity, BillingFlowParams params)
It will show the Google Play purchase screen. The result will be delivered via the PurchasesUpdatedListener interface implementation reported to the BillingClientImpl
constructor.
activity - An activity reference from which the billing flow will be launched.params - Params specific to the request BillingFlowParams).BillingClient.BillingResponse) of launch flow operation.@UiThread public abstract void launchPriceChangeConfirmationFlow(Activity activity, PriceChangeFlowParams params, @NonNull PriceChangeConfirmationListener listener)
When the price of a user subscribed item has changed, launch this flow to take users to a screen with price change information. User can confirm the new price or cancel the flow.
activity - An activity reference from which the billing flow will be launched.params - Params specific to the request BillingFlowParams).listener - Implement it to get the result of your price change flow.public abstract Purchase.PurchasesResult queryPurchases(String skuType)
Note: It's recommended for security purposes to go through purchases verification on your backend (if you have one) by calling the following API: https://developers.google.com/android-publisher/api-ref/purchases/products/get
skuType - The type of SKU, either "inapp" or "subs" as in BillingClient.SkuType.Purchase.PurchasesResult containing the list of purchases and the
response code (BillingClient.BillingResponsepublic abstract void querySkuDetailsAsync(SkuDetailsParams params, @NonNull SkuDetailsResponseListener listener)
params - Params specific to this query request SkuDetailsParams.listener - Implement it to get the result of your query operation returned asynchronously
through the callback with the BillingClient.BillingResponse and the list of SkuDetails.public abstract void consumeAsync(String purchaseToken, @NonNull ConsumeResponseListener listener)
Consumption is done asynchronously and the listener receives the callback specified upon completion.
purchaseToken - The purchase token of the item to consume.listener - Implement it to get the result of your consume operation returned
asynchronously through the callback with token and BillingClient.BillingResponse parameters.public abstract void queryPurchaseHistoryAsync(String skuType, @NonNull PurchaseHistoryResponseListener listener)
skuType - The type of SKU, either "inapp" or "subs" as in BillingClient.SkuType.listener - Implement it to get the result of your query returned asynchronously through
the callback with a Purchase.PurchasesResult parameter.public abstract void loadRewardedSku(RewardLoadParams params, @NonNull RewardResponseListener listener)
If the rewarded sku is available, the response will be BILLING_RESULT_OK. Otherwise the response will be ITEM_UNAVAILABLE. There is no guarantee that a rewarded sku will always be available. After a successful response, only then should the offer be given to a user to obtain a rewarded item and call launchBillingFlow.
params - Params specific to this load request RewardLoadParamslistener - Implement it to get the result of the load operation returned asynchronously
through the callback with the BillingClient.BillingResponse