androidbilling:4.0.0-queryPurchases(INAPP)和purchase.getSku()

我刷新到 android billing 版本 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,我的 ifif (PRODUCT_ID.equals(purchase.getSkus()) && purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED)会说它总是错误的。而且我不知道如何使用 queryPurchasesAsync(),现在需要 2 个参数。

谢谢。

回答

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

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

旧方法的示例:

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);
        }
    });

  • As developers attempt upgrading billing to the new version 4.0.0 I expect this post will get a lot more notice. The new documentation is incomplete as usual, so we are on our own for updating missing details.

回答

getSkus 返回一个 ArrayList。请使用“包含”如下。

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

  • why do we need to use .toLowerCase()??

以上是androidbilling:4.0.0-queryPurchases(INAPP)和purchase.getSku()的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>