其他分享
首页 > 其他分享> > android-InAppBilling v3 IabResult响应代码BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED

android-InAppBilling v3 IabResult响应代码BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED

作者:互联网

根据开发人员站点上的文档,我最近在我的应用程序中实现了InAppBilling v3.我使用了TRIVIAL DRIVE示例中提供的utils包中的类.

我面临的问题是,如果用户已在另一台设备上再次启动购买流程时已经购买了应用程序内产品,则播放商店对话框显示ITEM ALREADY OWNED,但IabResult返回的响应代码与常量IabHelper.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED不匹配.返回的响应代码实际上是IabHelper类中的错误代码之一(-1005用户已取消).

我真的很想知道如何获取实际的响应代码而不是错误代码.任何帮助,将不胜感激.

下面是回调的代码

// Callback for when a purchase is finished
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener =
        new IabHelper.OnIabPurchaseFinishedListener() {
            public void onIabPurchaseFinished(IabResult result, Purchase purchase) {

                if (result.isFailure()) {
                    if (result.getResponse() ==
                            IabHelper.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) {
                        //already owned
                        boolean isPremium = true;
                        SharedPrefsUtils.setPremium(BaseActivity.this, isPremium);
                        EventBus.getDefault().post(new InAppBillingUiUpdateEvent(isPremium));
                        //setWaitScreen(false);
                        return;
                    }
                    //handle error
                    complain(result.getResponse() + " " + "Error purchasing: " + result);
                    //setWaitScreen(false);
                    return;
                }
                if (!verifyDeveloperPayload(purchase)) {
                    //corrupted
                    complain("Error purchasing. Authenticity verification failed.");
                    //setWaitScreen(false);
                    return;
                }

                //successful
                if (purchase.getSku().equals(NO_ADS_PRODUCT_ID)) {
                    // bought the premium upgrade!
                    alert("Thank you for upgrading to premium!");
                    boolean isPremium = true;
                    SharedPrefsUtils.setPremium(BaseActivity.this, isPremium);
                    EventBus.getDefault().post(new InAppBillingUiUpdateEvent(isPremium));
                    //setWaitScreen(false);
                }
            }
        }; 

解决方法:

我终于设法在IabHelper代码中找到了问题,所以只要在handleActivityResult方法中返回Activity.RESULT_CANCELED结果代码,无论原因为何,用户取消(-1005)都可以修复IabResult固定的所有情况下的问题.因此,为了获得正确的实际响应代码,请在handleActivityResult中替换以下代码

 else if (resultCode == Activity.RESULT_CANCELED) {
        logDebug("Purchase canceled - Response: " + getResponseDesc(responseCode));
        result = new IabResult(IABHELPER_USER_CANCELLED, "User canceled.");
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
    }

有了这个

 else if (resultCode == Activity.RESULT_CANCELED) {
        logDebug("Purchase canceled - Response: " + getResponseDesc(responseCode));
        result = new IabResult(responseCode, null);
        if (mPurchaseListener != null) mPurchaseListener.onIabPurchaseFinished(result, null);
    }

希望可以节省别人的时间

标签:in-app-purchase,in-app-billing,in-app,android
来源: https://codeday.me/bug/20191028/1949637.html