簡體   English   中英

應用內結算解鎖按鈕只有一次

[英]In-app Billing unlock button only one time

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

嗨,我的應用程序有問題。 情況是:我希望人們在我的應用程序中付款時解鎖一個按鈕,但我只想支付一次,但當他們付款時,他們只解鎖一次按鈕。 對不起,我的英語不好。 這是我的源代碼

你的代碼有點亂,但這是你的流程:

  • 當用戶點擊按鈕時,您開始購買流程
  • 當采購流程結束時,您查詢采購的物品清單
  • 如果購買的物品存在,你就消費它

當你消費一個物品時,你“刪除”了它,所以它不再出現在購買的物品列表中。 如果你想只賣一次東西(例如刪除廣告),你就不必消費它。

你的流程應該是這樣的:

  • 查詢購買的物品
  • 如果列表包含購買項目,請禁用該按鈕
  • 如果列表不包含購買項目,啟用按鈕
  • 點擊后,開始購買流程
  • 當采購流程結束時,您查詢采購的物品清單
  • 如果購買的物品存在,禁用按鈕並提供額外的功能
  • 如果購買的物品不存在,則購買程序失敗

您應該仔細閱讀此頁面: https : //developer.android.com/training/in-app-billing/purchase-iab-products.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM