【问题标题】:When do i need the IabHelper Class for making an InApp purchase?我什么时候需要 Iab 助手类来进行应用内购买?
【发布时间】:2014-06-11 09:06:05
【问题描述】:

我按照此文档http://developer.android.com/google/play/billing/billing_integrate.html 创建了一个测试应用程序,该应用程序在单击按钮后启动应用内购买程序。
我将应用程序发布为 alpha 版本并测试了应用内购买,它可以正常工作。

我的问题是,我对其他应用内购买示例感到困惑。他们都使用一个名为“IabHelper”的类来进行应用内购买。 但似乎不需要这个类来进行应用内购买。 该类甚至在官方 In-App-Billing V3 “TrivalDrive” 示例中使用,但在我上面发布的应用内计费指南中从未提及。

那么有人可以向我解释一下,为什么以及何时应该使用 IabHelper 类?

提前谢谢你。

这是我用来进行应用内购买的代码的简化版本:

private void doPurchase() {
    try {
        Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(), "premium.upgrade",
            "inapp", "");

        if (buyIntentBundle != null && buyIntentBundle.getInt("RESPONSE_CODE") == 0) {
            PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");

            startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(),
                Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
        }
    } catch (SendIntentException e) {
        e.printStackTrace();
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1001) {
        String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");

        if (resultCode == RESULT_OK) {
            try {
                JSONObject jo = new JSONObject(purchaseData);
                String sku = jo.getString("productId");
                Log.i(">>>", "You have bought the "
                    + sku
                    + ". Excellent choice, "
                    + "adventurer!");
            } catch (JSONException e) {
                Log.e(">>>", "Failed to parse purchase data.");
                e.printStackTrace();
            }
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

    你说得对,考虑到 IABHelper,指南有点令人困惑。基本上,IAB-Helper 是一个由 Google 员工编写的助手类,它完成了很多繁重的工作。因此,如果您使用 IAB-Helper,则不必像在 doPurchase() 方法中那样编写那么多代码。您只需编写以下行:

    mHelper.launchPurchaseFlow(activity, sku, RC_REQUEST, mPurchaseFinishedListener);
    

    购买后的东西也一样,不用摆弄 JSON 对象

    if (info.getSku().equals(sku))
        doSomething();
    

    注意:您必须先设置 mHelper 并实现各种侦听器。当然,IABHelper 在后台执行的操作与您在方法中执行的操作完全相同。这只是另一层抽象。

    这就是 IABHelper 的好消息。坏消息:IABHelper 有一些非常严重的错误,会导致偶尔崩溃。虽然我仍然建议使用它,因为它会捕获更多你甚至无法想到的错误。

    在以下 Android 开发人员培训网站上,您可以找到一个指南,该指南展示了如何很好地使用 IABHelper。 https://developer.android.com/training/in-app-billing/preparing-iab-app.html#GetSample

    【讨论】:

    • 当。谷歌甚至无法编写一个没有错误的应用内购买实现,这真是太可悲了。
    • @trans 是的,这有点奇怪,因为它是平台的基本组成部分。尽管如此,我已经使用一个名为Checkout 的库已经有一段时间了,但没有遇到任何明显的问题。我强烈推荐它用于与 IAB 相关的所有内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多