![](/img/trans.png)
[英]How do I revoke an Android in-app non-consumable purchase entitlement after refunding?
[英]How do I check if an item (Non-Consumable in-app purchase ) is already owned or not programmatically in Android Studio?
我想知道我是否可以檢查已經擁有的非消耗性應用內購買。 請幫我。
是的,您可以使用 android BillingClient檢查是否已擁有的non-consumable
In-App Purchase
您必須在build.gradle
中添加以下dependency
項
implementation 'com.android.billingclient:billing:1.2'
並在您的AndroidManifest.xml
文件中使用以下permission
。
<uses-permission android:name="com.android.vending.BILLING" />
您可以通過在mBillingClient
上運行queryPurchaseHistoryAsync
回調來獲取當前用戶in-app purchase
歷史記錄。
這是我如何在我的項目中實現它的示例。 希望它可以幫助你。
private BillingClient mBillingClient;
private SkuDetailsParams.Builder params;
private SkuDetails skuDetails;
protected void getPurchaseHistory() {
mBillingClient = BillingClient.newBuilder(this).setListener(this).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.
Log.d(TAG, "onBillingSetupFinished ");
mBillingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS,
new PurchaseHistoryResponseListener() {
@Override
public void onPurchaseHistoryResponse(@BillingClient.BillingResponse int responseCode,
List<Purchase> purchasesList) {
if (responseCode == BillingClient.BillingResponse.OK
&& purchasesList != null) {
boolean productFound = false;
for (Purchase purchase : purchasesList) {
// check is user purchased your product or not
if (purchase.getSku().equals("your in-app product id")) {
//success! your user has bought your In-App product. Woohoo!
productFound = true;
// you can check user subscription details by using purchase token
checkSubscription(purchase.getPurchaseToken());
prefs().setPurchaseToken(purchase.getPurchaseToken());
}
}
if (!productFound) {
// user not purchase your product yet.
prefs().setIsSubscribed(false);
Log.d(TAG, "product not found");
}
} else {
prefs().setIsSubscribed(false);
Log.d(TAG, "Purchase history response not ok");
}
}
});
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Log.d(TAG, "onBillingServiceDisconnected ");
}
});
}
快樂編碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.