首次启动时检查 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 cannot be resolved" 错误的原因是因为您的示例中引用的 以令人困惑的方式交换了两行。

必须提及:Google 显然不再支持 IabHelper;您应该改用 billing client library