gpt4 book ai didi

android - 我如何在适用于 Android 的 Google Play Billing 中获取有关应用内购买的信息

转载 作者:行者123 更新时间:2023-12-04 01:42:32 27 4
gpt4 key购买 nike

我想了解如何在 android studio 中获取已购买产品的详细信息,无论是否有互联网连接。

据我所知,在 google play 计费库中有两种检索信息的方法。

1. PurchasesResult purchasesResult = billingClient.queryPurchases(SkuType.INAPP);

2. billingClient.queryPurchaseHistoryAsync(SkuType.INAPP,
new PurchaseHistoryResponseListener() {
@Override
public void onPurchaseHistoryResponse(BillingResult billingResult,
List<Purchase> purchasesList) {
if (billingResult.getResponseCode() == BillingResponse.OK
&& purchasesList != null) {
for (Purchase purchase : purchasesList) {
// Process the result.
}
}
}
});

但我不知道如何检索信息。

最佳答案

This is the way I have implemented In-App Purchase. It is working smoothly. Hope it helps.

mBillingClient = BillingClient.newBuilder(this).setListener(new PurchasesUpdatedListener() {
@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {

// Log.d("anupam", responseCode + "");
if (responseCode == BillingClient.BillingResponse.OK && purchases != null) {
for (Purchase purchase : purchases) {
//List of purchases
}
} else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
Toast.makeText(SplashActivity.this, "Sorry, you have canceled purchase Subscription.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SplashActivity.this, "Sorry, some error occurred.", Toast.LENGTH_SHORT).show();
}
}
}).build();

mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {

if (billingResponseCode == BillingClient.BillingResponse.OK) {
// The billing client is ready. You can query purchases here.

final Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.SUBS);
for (Purchase sourcePurchase : purchasesResult.getPurchasesList()) {
if (sourcePurchase != null) {

ConsumeResponseListener listener = new ConsumeResponseListener() {
@Override
public void onConsumeResponse(final int responseCode, final String purchaseToken) {
// Log.d("anupam2", responseCode + " <-------> "+ purchasesResult.getPurchasesList() + " <-------> " + purchaseToken);
}
};
mBillingClient.consumeAsync(sourcePurchase.getPurchaseToken(), listener);
} else {

}
}

if (purchasesResult.getPurchasesList().size() > 0) {
// Log.d("anupam3", purchasesResult.getPurchasesList().size() + "");

} else {
// Log.d("anupam4", purchasesResult.getPurchasesList().size() + "");
BillingFlowParams.Builder builder = BillingFlowParams.newBuilder().setSku("234r23446").setType(BillingClient.SkuType.SUBS);
int responseCode = mBillingClient.launchBillingFlow(SplashActivity.this, builder.build());
if (responseCode == 7) {

//Item already purchased
}
}
}
}

@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Toast.makeText(SplashActivity.this, "Failed", Toast.LENGTH_LONG).show();
}
});

关于android - 我如何在适用于 Android 的 Google Play Billing 中获取有关应用内购买的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56787791/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com