Android - queryPurchaseHistoryAsync 购买列表在其他设备上为空

标签 android in-app-purchase in-app-billing

我正在尝试在我的应用中实现新的 Google Play 结算服务,并尝试使用 queryPurchaseHistoryAsync() 方法和 Purchase 检索我已购买的应用内商品 列表始终为空且包含 0 个元素。

代码在我购买该商品的设备上运行良好,但在我具有相同 Google 帐户的其他设备上,它没有返回任何内容。

也与 Documentation 中一样, queryPurchaseHistoryAsync() 应该与 Google 同步以获取购买历史记录,但由于某种原因它似乎没有同步。

我的代码是:

BillingClient billingClient = BillingClient.newBuilder(getApplicationContext()).setListener(new PurchasesUpdatedListener() {
    @Override
    public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
        if(responseCode == BillingClient.BillingResponse.OK) {
            //Do something
        }
    }
}).build();
billingClient.startConnection(new BillingClientStateListener() {
    @Override
    public void onBillingSetupFinished(int responseCode) {
        if(responseCode == BillingClient.BillingResponse.OK) {
            //Response is OK and working fine
        } 
    }

    @Override
    public void onBillingServiceDisconnected() {
        //Do something
    }
});

billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.INAPP, new PurchaseHistoryResponseListener() {
    @Override
    public void onPurchaseHistoryResponse(int responseCode, List<Purchase> purchasesList) {
        if(responseCode == BillingClient.BillingResponse.OK) {
            //Always returning 0 size() of purchasesList
            Toast.makeText(getApplicationContext(), "There are " + purchasesList.size() + " items you've purchased.", Toast.LENGTH_LONG).show();
        }
    }
});

这段代码究竟哪里出错了?

非常感谢。

最佳答案

您是否在 onBillingSetupFinished() 之后调用了 queryPurchaseHistoryAsync()

在您的源代码上调用 onBillingSetupFinished() 之前,您似乎确实调用了 queryPurchaseHistoryAsync()。

您可以在this doc中查看以下代码

private BillingClient mBillingClient;
...
mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();
mBillingClient.startConnection(new BillingClientStateListener() {
    @Override
    public void onBillingSetupFinished(@BillingResponse int billingResponseCode) {
        if (billingResponseCode == BillingResponse.OK) {
            // The billing client is ready. You can query purchases here.

        }
    }
    @Override
    public void onBillingServiceDisconnected() { }
});

关于Android - queryPurchaseHistoryAsync 购买列表在其他设备上为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50941340/

相关文章:

android - AdMob 广告单元 ID 需要多长时间才能生效?

java - Android 生命周期问题

android - 多 Intent 附加

android - 发布android后更改应用内购买项目的价格

Android 应用内支付 - 重装应用后 repo

android - 无法更改应用内结算的默认货币

android - 使用 libgdx 播放动画 gif

ios - iOS应用程序中的订阅必需

ios8 - 为什么 SKStoreProductViewController loadProductWithParameters 不会在 iOS 8.0.2 上为 Apple 应用测试人员调用其completionBlock?

android - 应用内计费 V3