检查是否应用内购买在Android的拥有第一次启动(Checking if in-app purch

2019-10-30 05:03发布

我想检测如果任何应用程序内购买的是由用户,当应用程序被启动的第一次尝试更新使用SharedPreferences应用的专业模式拥有。 下面的代码是不幸的是没有工作:(

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

我不知道什么是错的这个代码。 预先感谢您的帮助和新年快乐! :)

Answer 1:

你必须调用IabHelper.queryInventoryAsync()为了使QueryInventoryFinishedListener做任何有用的东西。 只是该函数的调用立即您之前添加startActivity()调用。 (这是假设你已经叫IabHelper.startSetup()和所有的好东西第一。)

你可以不前的声明指的是局部变量。 究其原因,你得到了一个“mReceivedInventoryListener不能得到解决”的错误是因为答案在你的例子引用的一个令人困惑的方式交换了两行。

强制性提到: IabHelper显然不再通过谷歌的支持; 你应该使用计费客户端库来代替。



文章来源: Checking if in-app purchases are owned in Android on first launch