android billing:4.0.0 - queryPurchases(INAPP) and purchase.getSku()

我刷新到Android计费版本4和2的东西不再工作。

首先我有这个:

else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) {
            Purchase.PurchasesResult queryAlreadyPurchasesResult = billingClient.queryPurchases(INAPP); // deprecated
            List<Purchase> alreadyPurchases = queryAlreadyPurchasesResult.getPurchasesList();
            if(alreadyPurchases!=null){
                handlePurchases(alreadyPurchases);
            }
        }

queryPurchases 已被弃用。

其次,我有这个:

void handlePurchases(List<Purchase>  purchases) {
    for(Purchase purchase:purchases) {
        //if item is purchased
        if (PRODUCT_ID.equals(purchase.getSku()) && purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED)
        {
            if (!verifyValidSignature(purchase.getOriginalJson(), purchase.getSignature())) {
                // Invalid purchase
                // show error to user
                Toast.makeText(getApplicationContext(), R.string.plus_error, Toast.LENGTH_SHORT).show();
                return;
            }

getSku() 工作正常,但现在它被标记为Cannot resolve method getSku() in Purchase

有什么想法如何解决这个问题?


来自文档:

Summary of changes
Added BillingClient.queryPurchasesAsync() to replace BillingClient.queryPurchases() which will be removed in a future release.

Added Purchase#getSkus() and PurchaseHistoryRecord#getSkus(). These replace Purchase#getSku and PurchaseHistoryRecord#getSku which have been removed.

但是我不知道如何在上面的代码中应用这个新命令。

如果我将getSku更改为getSkus,我的if会说它总是假的。我不知道如何使用queryPurchasesAsync(),现在需要2个参数。if (PRODUCT_ID.equals(purchase.getSkus()) && purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED)

谢谢。


答案 1

正如我之前在评论中提到的,您正在将String与List对象进行比较,但正如chitgoks所说,它是,而不是像我假设的那样。我不确定你是否会得到一个以上的sku-string(因为你可能不会同时订购多个东西?),但要么把它们都看一遍以确定,要么抓住机会,PRODUCT_ID与购买.getSkus().get(0)进行比较。ArrayList<String>List<String>

新的异步购买调用似乎只需要很小的更改。

旧方法的示例:

Purchase.PurchasesResult result = billingClient.queryPurchases(BillingClient.SkuType.SUBS);
doSomethingWithPurchaseList(result.getPurchasesList());

这将是做同样的事情的新方法:

billingClient.queryPurchasesAsync(BillingClient.SkuType.SUBS, new PurchasesResponseListener() {
        @Override
        public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List<Purchase> list) {
            doSomethingWithPurchaseList(list);
        }
    });

答案 2

getSkus 返回一个 .请使用如下。ArrayList<String>contains

purchase.getSkus().contains(YOUR_PRODUCT_ID.toLowerCase())

推荐