Wednesday, August 29, 2012

購買in-app billing,Google Play視窗開了2次

今天遇到一件事,
至從增加Subscription服務後,
Dungeons在request購買時,
畫面會先跳出找不到項目,
然後又跳出in-app billing商品列表,
I encountered a problem when I was using Google In-app billing today that is when I request purchase, Google Play Billiing view shows twice.
First, It shown "Can't find Item" dialog then shown me the in-app billing product item what I set.

爬文才發現問題出在Google Play Billing的Sample Code:Dungeons.java出了一個邏輯上的錯誤,
那就是︰
Finally I figure out the cause is logic in in-app billing sample code:Dungeons.java.
Wrong code is on below:


 @Override
    public void onClick(View v) {
        if (v == mBuyButton) {
            if (Consts.DEBUG) {
                Log.d(TAG, "buying: " + mItemName + " sku: " + mSku);
            }

            if (mManagedType != Managed.SUBSCRIPTION &&
                    !mBillingService.requestPurchase(mSku, Consts.ITEM_TYPE_INAPP, mPayloadContents)) {
                showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
            } else if (!mBillingService.requestPurchase(mSku, Consts.ITEM_TYPE_SUBSCRIPTION, mPayloadContents)) {
                // Note: mManagedType == Managed.SUBSCRIPTION
                showDialog(DIALOG_SUBSCRIPTIONS_NOT_SUPPORTED_ID);
            }

        } else if (v == mEditPayloadButton) {
            showPayloadEditDialog();
        } else if (v == mEditSubscriptionsButton) {
            editSubscriptions();
        }
    }

紅色那段的邏輯錯誤,
造成Google Play視窗被開啟兩次,
只要將那段改成
 if (mManagedType != Managed.SUBSCRIPTION &&
                    !mBillingService.requestPurchase(mSku, Consts.ITEM_TYPE_INAPP, mPayloadContents)) {
                showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
            } else if (mManagedType == Managed.SUBSCRIPTION && !mBillingService.requestPurchase(mSku, Consts.ITEM_TYPE_SUBSCRIPTION, mPayloadContents)) {
                // Note: mManagedType == Managed.SUBSCRIPTION
                showDialog(DIALOG_SUBSCRIPTIONS_NOT_SUPPORTED_ID);
            }
即可改善Billing視窗開了2次的問題。 
If modify code as above will fix this bug.

2 comments:

Jason Ko said...

小縵, 我用Sample code時, 除了有這個問題,
還出現 "這一版的應用程式不提供Google Play結帳".
可是我已經是最新的sample code了 @@

Jason Ko said...

喔喔 可能我沒設另一個帳號