如何设置应用内购买(非消耗品)? [英] How to setup In App purchase (Non Consumable)?

查看:129
本文介绍了如何设置应用内购买(非消耗品)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些在线教程在我的应用中实施应用内购买.但是该教程适用于应用内购买".但就我而言,用户只需要购买一次.

I am Implementing In App purchase in my app, using some online tutorial. But the tutorial is for Consumable In App Purchase. But in my case, Users need to buy only once.

我修改了代码,以在购买In App Purchase后禁用购买按钮".现在一切正常.但是问题是,如果我关闭并打开该应用,购买按钮"就会启用.

I modified the Code, to disable the "Buy Button" after buying the In App Purchase. Now it's working fine. But the problem is if I close and open the app, "Buy button" getting enabled.

这是我的xml代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".InAppBillingActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/click_string"
        android:id="@+id/clickButton"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="113dp"
        android:onClick="buttonClicked"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/buy_string"
        android:id="@+id/buyButton"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="buyClick" />
</RelativeLayout>

这是我的应用内结算活动

This is my In App Billing Activity

public class InAppBillingActivity extends AppCompatActivity {

    private static final String TAG =
            "InAppBilling";
    IabHelper mHelper;
    static final String ITEM_SKU = "com.example.buttonclick";

    private Button clickButton;
    private Button buyButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_in_app_billing);

        buyButton = (Button)findViewById(R.id.buyButton);
        clickButton = (Button)findViewById(R.id.clickButton);
        clickButton.setEnabled(false);
        String base64EncodedPublicKey =
                "<place your public key here>";

        mHelper = new IabHelper(this, base64EncodedPublicKey);

        mHelper.startSetup(new
                                   IabHelper.OnIabSetupFinishedListener() {
                                       public void onIabSetupFinished(IabResult result) {
                                           if (!result.isSuccess()) {
                                               Log.d(TAG, "In-app Billing setup failed: " +
                                                       result);
                                           } else {
                                               Log.d(TAG, "In-app Billing is set up OK");
                                           }
                                       }
                                   });
    }

    public void buttonClicked (View view)
    {
        Intent Quiz = new Intent(getApplicationContext(), QuestionYearwises.class);
        startActivity(Quiz);
    }

    public void buyClick(View view) {
        mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
                mPurchaseFinishedListener, "mypurchasetoken");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data)
    {
        if (!mHelper.handleActivityResult(requestCode,
                resultCode, data)) {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
            = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result,
                                          Purchase purchase)
        {
            if (result.isFailure()) {
                // Handle error
                return;
            }
            else if (purchase.getSku().equals(ITEM_SKU)) {
                consumeItem();
                buyButton.setEnabled(false);
            }

        }
    };
    public void consumeItem() {
        mHelper.queryInventoryAsync(mReceivedInventoryListener);
    }

    IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
            = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result,
                                             Inventory inventory) {

            if (result.isFailure()) {
                // Handle failure
            } else {
                mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                        mConsumeFinishedListener);
            }
        }
    };

    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
            new IabHelper.OnConsumeFinishedListener() {
                public void onConsumeFinished(Purchase purchase,
                                              IabResult result) {

                    if (result.isSuccess()) {
                        clickButton.setEnabled(true);
                    } else {
                        // handle error
                    }
                }
            };

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


}

推荐答案

您的用户每次打开应用程序时,要做的第一件事就是检查购买状态.如果用户已经拥有该购买,请在您的应用中释放与购买相关的功能,然后将按钮的可见性设置为View.GONE或对您有意义的东西,我个人就是将其隐藏.

Every time your users open the app, the first thing you want to do is to check for the status of the purchase. If the user already owns the purchase, release the features in your app related to the purchase, and then just set your button visibility to View.GONE or something meaningful to you, personally I just hide it.

如果是一次性购买,则一定不要消费,就像在mPurchaseFinishedListener中正在消费一样,然后当用户打开应用程序时检查商品的所有权时,这似乎是递归的购买,因为您已经消费了该商品,它将允许您再次购买.

You must not consume your purchase if it is a one time purchase, seems like in your mPurchaseFinishedListener you are consuming it, then when you check for ownership of the item when the user opens the app it seems like is a recursive purchase as you already consumed the item, it will allow you to buy again.

如果可能,您可以考虑使用 Google Play计费库.您使用的是带有AIDL的版本,它将被弃用请参见.

You may want to consider using the Google Play Billing Library, if the one you are using is the one with AIDL it will be deprecated see.

这是 Codelab ,可以帮助您入门使用 Google Play结算库

This is a Codelab to get you started with the Google Play Billing Library

这篇关于如何设置应用内购买(非消耗品)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆