调用时如何在Java中使用.setSkuDetails(skuDetails)
BillingFlowParams flowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetails)
.build();
int responseCode = billingClient.launchBillingFlow(flowParams);
我搜索了整个google,但未找到任何适当且完整的实现,请有人通过提供具有所有必要功能的完整过程来帮助我,因为我只找到library
1.0
示例或Kotlin或不完整,所以我想使用java请帮助我
最佳答案
这是完整的实现。
有关更多参考,请参考文档:https://developer.android.com/google/play/billing/billing_library_overview
String ITEM_SKU_diamond_500 = "diamond_500";
BillingClient billingClient;
AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener;
String premiumUpgradePrice = "";
1:必须使用
BillingClient.Builder
创建计费客户端。billingClient = BillingClient.newBuilder(this)
.enablePendingPurchases()
.setListener(this).build();
2:创建billingClient之后,启动billingClient连接
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
List<String> skuList = new ArrayList<>();
skuList.add(ITEM_SKU_diamond_500);
final SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
3:建立成功的连接后,使用billingClient的querySkuDetailsAsync方法进行调用以异步获取“ sku”详细信息。
billingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {
if (skuDetailsList != null && billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
for (SkuDetails skuDetails : skuDetailsList) {
String sku = skuDetails.getSku();
String price = skuDetails.getPrice();
final BillingFlowParams params = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetails)
.build();
if (ITEM_SKU_diamond_500.equals(sku)) {
premiumUpgradePrice = price;
firstBtn500(params);
}
}
} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.ERROR) {
Toast.makeText(DiamondsActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
}
});
} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.SERVICE_TIMEOUT) {
Toast.makeText(DiamondsActivity.this, "Service timeout", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(DiamondsActivity.this, "Failed to connect to the billing client", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onBillingServiceDisconnected() {
Toast.makeText(DiamondsActivity.this, "Disconnected from the client", Toast.LENGTH_SHORT).show();
}
});
acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
Toast.makeText(DiamondsActivity.this, "Purchase acknowledged", Toast.LENGTH_SHORT).show();
}
};
4:当用户尝试购买应用内或订阅产品时,请使用
billngClient's isFeatureSupported(BillingClient.FeatureType
./* SUBSCRIPTIONS或其他* /)方法检查该产品是否受支持,并调用billingClient的launchBillingFlow方法private void firstBtn500(final BillingFlowParams params) {
firstPurchaseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
billingClient.launchBillingFlow(DiamondsActivity.this, params);
}
});
}
更新
在这里您可以检查商品是否已经购买
@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
if (purchases != null && billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
for (Purchase purchase : purchases) {
handlePurchases(purchase);
}
} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
Toast.makeText(this, "Purchased Canceled", Toast.LENGTH_SHORT).show();
}
else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) {
Toast.makeText(this, "Already Purchased", Toast.LENGTH_SHORT).show();
}
}
如果您要购买一次商品,则需要
Acknowledge
购买private void handlePurchases(final Purchase purchase) {
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
//Acknowledge the purchase if it hasn't already been acknowledged.
if (!purchase.isAcknowledged()) {
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
billingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
}
如果您想一次又一次地购买同一物品(例如应用程序/游戏中的硬币或信用卡或类似的东西)。您需要消费购买以下是消费购买的代码。
// Todo :Consume the purchase async
ConsumeParams consumeParams = ConsumeParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
ConsumeResponseListener consumeResponseListener = new ConsumeResponseListener() {
@Override
public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
Toast.makeText(DiamondsActivity.this, "Purchase successful", Toast.LENGTH_SHORT).show();
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
if (purchase.getSku().equalsIgnoreCase(ITEM_SKU_diamond_500)) {
Toast.makeText(DiamondsActivity.this, "Thank you for purchasing!", Toast.LENGTH_SHORT).show();
}
}
}
};
billingClient.consumeAsync(consumeParams, consumeResponseListener);
} else if (purchase.getPurchaseState() == Purchase.PurchaseState.PENDING) {
Toast.makeText(this, "Purchase pending", Toast.LENGTH_SHORT).show();
}
}