Android - 确定应用程序内购买是否成功无法正常工作的方法

时间:2015-09-28 03:12:23

标签: android in-app-purchase in-app-billing

  

我已经能够在我的android中实现应用程序内购买   应用程序(按照android的步骤说明进行操作   开发者网站   https://developer.android.com/intl/es/training/in-app-billing/index.html),   但是假设回调发生了一些奇怪的事情   表明购买是否成功。我正在接受“购买   成功的“消息包含真实和静态产品ID,但是   在我打电话之前,我希望看到的变化不会发生   单击我的升级按钮再次“Iab.QueryInventoryFinishedListener”。从我在那里读到的   似乎是“onIabPurchaseFinished”的一个问题。我试过用了   解决方案提供给下面链接中的问题,但是   “onActivityResult”也没有做到这一点。我正在表演这些   片段中的动作,我没有看到任何错误,但我不确定   如果这可能是问题的一部分。任何帮助都会非常多   赞赏。提前谢谢。

     

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

1 个答案:

答案 0 :(得分:0)

我能够解决问题。事实证明" onActivityResult"需要在我正在工作的片段的父活动中。我重新定位它(连同mHelper代码,即设置和销毁)但保留了我的片段中的代码示例中显示的其他方法,并且能够得到& #34; onIabPurchaseFinished"调用自定义方法" purchaseSuccess();"

在阅读了关于这个问题的答案的评论后,想出了这个:IabHelper PurchaseFinishedListener