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

Ahm*_*med 8 android in-app-purchase in-app-billing

我正在尝试将新的Google Play结算应用到我的应用中,并尝试使用queryPurchaseHistoryAsync()方法来检索我已经购买的应用内商品,并且该Purchase列表始终为0,且元素为空。

该代码在我购买了该商品的设备上运行正常,但是在其他具有相同Google帐户的设备上,该代码什么也没有返回。

就像在文档中一样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();
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

我在哪里弄错了这段代码?

非常感谢。

2yo*_*yun 8

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

似乎您在源代码上调用onBillingSetupFinished()之前先调用queryPurchaseHistoryAsync()。

您可以在此文档中检查以下代码

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() { }
});
Run Code Online (Sandbox Code Playgroud)