简体   繁体   中英

Android - Method(s) to determine if In-App purchase is successful not working properly

I have been able to implement In-App purchases within my android application (following step by step instructions from the android developer site https://developer.android.com/intl/es/training/in-app-billing/index.html ), but something strange is happening with the callback that is supposed to indicate if the purchase is successful. I am getting the "Purchase Successful" message with both real and static product ID's, but the changes I expect to see are not made until I call "Iab.QueryInventoryFinishedListener" again by clicking my upgrade button. From what I've read there seems to be a problem with "onIabPurchaseFinished". I've tried using the solution provided to the question in the link below, but "onActivityResult" isn't doing the trick either. I am performing these actions in a fragment, and I'm not seeing any errors, but I'm not sure if that might be part of the problem. Any help would be very much appreciated. Thanks in advance.

onIabPurchaseFinished never called.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_game, container, false);

    mHelper = new IabHelper(getActivity(), base64EncodedPublicKey);
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                // Oh noes, there was a problem.
                Log.d(TAG, "Problem setting up In-app Billing: " + result);
            }
            // Hooray, IAB is fully set up!
        }
    });

    return rootView;
}

@Override
    public void onDestroy() {
        super.onDestroy();
        if (mHelper != null) mHelper.dispose();
        mHelper = null;
    }

@Override
public void onClick(View button) {
if (button == upgrade){
        if (upgradeOn) {
            //Button Animation
            final Animation animation = buttonAnimation();
            upgrade.startAnimation(animation);

            upgradeOn = false;

            //Check for upgrade
            mHelper.queryInventoryAsync(mGotInventoryListener);
        }
    }
}

//1.
    //Check For Previous Purchases
    IabHelper.QueryInventoryFinishedListener mGotInventoryListener
            = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result,
                                             Inventory inventory) {

            if (result.isFailure()) {
                upgradeOn = true;
            }
            else {
                //Check for previous upgrade
                previousUpgrade = inventory.hasPurchase("android.test.purchased");
                if (previousUpgrade){
                    purchaseSuccess();
                }else {
                    //Continue with new purchase
                    mHelper.launchPurchaseFlow(getActivity(), "android.test.purchased", 811,
                            mPurchaseFinishedListener, "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ"
                                    + ParseUser.getCurrentUser().getUsername());
                }
            }
        }
    };

    //2.
    //Handle Purchase
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
            super.onActivityResult(requestCode, resultCode, data);
        }
        if (requestCode == 811){
            Log.e("TEST: ","AAAAHHH!");
            if (resultCode == 0){
                Log.e("TEST","URRGGGHHH!");
            }
        }
    }

    //3.
    //Purchase Complete
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
            = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase)
        {
            if (result.isFailure()) {
                return;
            }
            else if (purchase.getSku().equals("android.test.purchased")) {
                purchaseSuccess();
            }
        }
    };

I was able to fix the problem. It turns out that "onActivityResult" needed to be in the parent activity of the fragment I was working in. I relocated it (along with the mHelper code ie setup, and destroy) but kept the other methods shown in the code sample in my fragment, and was able to get "onIabPurchaseFinished" to call the custom method "purchaseSuccess();"

Figured this out after reading through the comments on the answer to this question: IabHelper PurchaseFinishedListener

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM