应用内结算解锁按钮只有一次

时间:2017-01-11 00:09:31

标签: java android in-app-purchase

 private Button clickButton;
    private Button buyButton;

    private static final String TAG =
            "InAppBilling";
    IabHelper mHelper;
    static final String ITEM_SKU = "tips";

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



        buyButton = (Button)findViewById(R.id.buybutton);
        clickButton = (Button)findViewById(R.id.clickbutton);
        clickButton.setEnabled(false);
        String base64EncodedPublicKey =
                "  "

        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 button2 (View v)
    {
        Intent intent = new Intent(getApplicationContext(), vtoriFra
                                   gment.class);
        startActivity(intent);
    }

    public void buttonClicked (View view)
    {
        clickButton.setEnabled(false);
        buyButton.setEnabled(true);
        Intent intent = new Intent(getApplicationContext(), purviFragment.class);
        startActivity(intent);
    }
    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;
    }
    @Override
    public void onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }

        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                doubleBackToExitPressedOnce=false;
            }
        }, 2000);
    }
}

嗨,我的应用程序遇到了麻烦。案例是: 我想当人们在我的应用程序中支付解锁一个按钮但我只想支付一个时间但是当他们支付时他们只解锁按钮一次。对不起,我的英语不好。这是我的源代码

1 个答案:

答案 0 :(得分:0)

你的代码有点混乱,但这是你的流程:

  • 当用户点击按钮时,您开始购买流程
  • 当购买流程结束时,您查询已购买商品的列表
  • 如果购买的商品存在,则使用它

当您使用某个项目时,您将其“删除”,因此在购买的项目列表中不再显示该项目。如果您只想销售一次(例如删除广告),则不必使用它。

你的流程应该是这样的:

  • 查询购买的商品
  • 如果列表包含购买项目,请禁用按钮
  • 如果列表中不包含购买项目,请启用按钮
  • 点击,开始购买流程
  • 当购买流程结束时,您查询已购买商品的列表
  • 如果购买的商品存在,请禁用该按钮并提供额外功能
  • 如果购买的商品不存在,则购买过程失败

您应该仔细阅读此页面: https://developer.android.com/training/in-app-billing/purchase-iab-products.html