我正在尝试对我的应用程序实现新的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()之后是否调用了QueryPurchaseHistorySync()?
在对源代码调用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() { }
});