build.gradle
dependencies { ... compile 'com.android.billingclient:billing:1.0' }
com.android.vending.BILLING
BillingClient
PurchasesUpdatedListener
onPurchasesUpdated()
@Override void onPurchasesUpdated(@BillingResponse int responseCode, List<Purchase> purchases) { if (responseCode == BillingResponse.OK && purchases != null) { for (Purchase purchase : purchases) { handlePurchase(purchase); } } else if (responseCode == BillingResponse.USER_CANCELED) { // Handle an error caused by a user canceling the purchase flow. } else { // Handle any other error codes. } }
mBillingClient = BillingClient.newBuilder(mContext) .setListener(mPurchasesUpdatedListener) .build();
querySkuDetailsAsync()
SkuDetailsResponseListener
onSkuDetailsResponse()
List<String> skuList = new ArrayList<> (); skuList.add("premiumUpgrade"); skuList.add("gas"); SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder(); params.setSkusList(skuList).setType(SkuType.INAPP); mBillingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() { @Override public void onSkuDetailsResponse(SkuDetailsResult result) { // Process the result. } })
launchBillingFlow()
SkuType.INAPP
BillingFlowParams
newBuilder()
BillingFlowParams.Builder builder = BillingFlowParams .newBuilder() .setSku(skuId).setType(SkuType.INAPP); int responseCode = mBillingClient.launchBillingFlow(builder.build());
purchaseToken
consumeAsync()
ConsumeResponseListener
onConsumeResponse()
ConsumeResponseListener listener = new ConsumeResponseListener() { @Override public void onConsumeResponse(@BillingResponse int responseCode, String outToken) { if (responseCode == BillingResponse.OK) { // Handle the success of the consume operation. // For example, increase the number of player's coins, // that provide temporary benefits } } }; mBillingClient.consumeAsync(purchaseToken, listener);