簡體   English   中英

在首次啟動時檢查應用內購買是否歸Android所有

[英]Checking if in-app purchases are owned in Android on first launch

我試圖在首次啟動應用程序時嘗試檢測用戶是否擁有任何應用程序內購買商品,並嘗試使用SharedPreferences續訂應用程序的Pro模式。 以下代碼不幸地不起作用:(

        if (version.equals("null")) { //checking version of the app, if it is unset equals first launch
        SharedPreferences.Editor editor = appinfo.edit();
        version = currentversion;
        editor.putString("version", version);
        editor.apply();

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

                if (mHelper == null) return; //IabHelper mHelper;
                Purchase purchase = Inventory.getPurchase("sku1");
                Purchase purchase2 = Inventory.getPurchase("sku2");
                Purchase purchase3 = Inventory.getPurchase("sku3");
                if (purchase != null || purchase2 != null || purchase3 != null) {
                    final SharedPreferences ispro = getApplicationContext().getSharedPreferences("ispro", 0);
                    SharedPreferences.Editor editor = ispro.edit();
                    editor.putInt("ispro", 1);
                    editor.apply();
                }
            }
        };

        startActivity(new Intent(MainPage.this, Changelog.class));

EDIT1:經過一些編輯后,代碼現在如下所示:

final List<String> skus = Arrays.asList("sku1", "sku2", "sku3");
    if (version.equals("null")) {
        SharedPreferences.Editor editor = appinfo.edit();
        version = currentversion;
        editor.putString("version", version);
        editor.apply();

        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            public void onIabSetupFinished(IabResult result) {

                if (!result.isSuccess()) {

                }


                if (mHelper == null) return;

                mBroadcastReceiver = new IabBroadcastReceiver(MainPage.this);
                IntentFilter broadcastFilter = new IntentFilter(IabBroadcastReceiver.ACTION);
                registerReceiver(mBroadcastReceiver, broadcastFilter);

                IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
                        = new IabHelper.QueryInventoryFinishedListener() {
                    public void onQueryInventoryFinished(IabResult result,
                                                         Inventory inventory) {
                        if (mHelper == null) return;
                        Purchase purchase = Inventory.getPurchase("sku1");
                        Purchase purchase2 = Inventory.getPurchase("sku2");
                        Purchase purchase3 = Inventory.getPurchase("sku3");
                        if (purchase != null || purchase2 != null || purchase3 != null) {
                            final SharedPreferences ispro = getApplicationContext().getSharedPreferences("ispro", 0);
                            SharedPreferences.Editor editor = ispro.edit();
                            editor.putInt("ispro", 1);
                            editor.apply();
                        }
                    }
                };

                try {
                     mHelper.queryInventoryAsync(true, skus, null, mReceivedInventoryListener);
                } catch (IabHelper.IabAsyncInProgressException e) {

                }
            }
        });

        startActivity(new Intent(MainPage.this, Changelog.class));

我不知道這段代碼有什么問題。 預先感謝您的幫助和新年快樂! :)

您必須調用IabHelper.queryInventoryAsync()才能使QueryInventoryFinishedListener進行有用的操作。 只需在調用startActivity()之前立即添加對該函數的調用。 (這是假設您已經調用了IabHelper.startSetup()並首先調用了所有這些好東西。)

您不能在聲明局部變量之前引用它。 出現“無法解析mReceivedInventoryListener”錯誤的原因是,示例中引用的答案以一種令人困惑的方式交換了兩行。

必須提及:Google顯然不再支持IabHelper 您應該改用計費客戶端庫

暫無
暫無

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

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