在首次启动时检查应用内购买是否归Android所有

时间:2018-12-31 18:13:21

标签: java android in-app-purchase in-app-billing billing

我试图在首次启动该应用程序时检测用户是否拥有该应用程序内购买项,然后尝试使用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));

我不知道此代码有什么问题。预先感谢您的帮助和新年快乐! :)

1 个答案:

答案 0 :(得分:1)

您必须呼叫IabHelper.queryInventoryAsync()才能使QueryInventoryFinishedListener做任何有用的事情。只需在您的startActivity()调用之前添加对该函数的调用即可。 (这是假设您已经先叫过IabHelper.startSetup(),然后把所有这些好东西都叫了。)

不能在声明局部变量之前引用它。您收到“无法解析mReceivedInventoryListener”错误的原因是,示例中引用的answer以一种令人困惑的方式交换了两行。

强制性提及:IabHelper显然不再受Google支持;您应该改用billing client library