如何检查我的应用程序的用户状态?在应用结算中

时间:2016-06-22 14:27:02

标签: android android-studio

我正在我的应用中实施In app Billing,让用户转到“应用的高级版”。在应用程序的高级版本中,用户可以单击3个按钮并使用其功能。但那不是问题。 问题是如何检查用户是否已购买“高级版”并使用所有应用功能? 这是我的代码:

private void promptForUpgrade() {
    AlertDialog.Builder upgradeAlert = new AlertDialog.Builder(this);
    upgradeAlert.setTitle("Upgrade?");
    upgradeAlert.setMessage("Do you want to upgrade to unlimited version?");
    upgradeAlert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //set progress dialog and start the in app purchase process
            upgradeDialog = ProgressDialog.show(selector.this, "Please wait", "Upgrade transaction in process", true);

              /* TODO: for security, generate your payload here for verification. See the comments on
     *        verifyDeveloperPayload() for more info. Since this is a SAMPLE, we just use
     *        an empty string, but on a production app you should carefully generate this. */
            String payload = "developerinnovaciones@gmail.com";


            try {
                mHelper.launchPurchaseFlow(selector.this, SKU_PREMIUM, RC_REQUEST,
                        mPurchaseFinishedListener, payload);
            } catch (IabHelper.IabAsyncInProgressException e) {
                e.printStackTrace();
            }

        }
    }).setNegativeButton("Nop", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    upgradeAlert.show();
}

这是OnIabPurchaseFinishedListener

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
        = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, Purchase purchase)
    {
        // if we were disposed of in the meantime, quit.
        if (mHelper == null) return;

        if (result.isFailure()) {
            alert("Error purchasing: " + result);
            upgradeDialog.dismiss();
        }


        else if (purchase.getSku().equals(SKU_PREMIUM)) {
            alert("Thank you for upgrade");
            mIsPremium = true;
            setUserStatus(true);
            upgradeDialog.dismiss();
        }

    }
};

所以基本上我想创建一个方法来验证用户是否是优质的。

希望你能帮助我:D

1 个答案:

答案 0 :(得分:0)

onCreate方法中,您应通过mHelper来检查mHelper.queryInventoryAsync设置中的购买状态。

mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
    Log.d(TAG, "Setup finished.");
    if (!result.isSuccess()) {
       // Oh noes, there was a problem.
       complain("Problem setting up in-app billing: " + result);
       isBillingSupported = false;
       return;
    }

  // Hooray, IAB is fully set up. Now, let's get an inventory of stuff we own.
  Log.d(TAG, "Setup successful. Querying inventory.");

  mHelper.queryInventoryAsync(false, mGotInventoryListener);

  isBillingSupported = true;
  }
});

mGotInventory我这样定义了

// Listener that's called when we finish querying the items we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result,
                                         Inventory inventory) {
        Log.d(TAG, "Query inventory finished.");
        if (result.isFailure()) {
            complain("Failed to query inventory: " + result);
            return;
        }

        Log.d(TAG, "Query inventory was successful.");

        // Do we have the premium upgrade?
        isPremium = inventory.hasPurchase(SKU_PREMIUM);
        Log.d(TAG, "User is " + (isPremium ? "PREMIUM" : "NOT PREMIUM"));

        updateUi();
        Log.d(TAG, "Initial inventory query finished; enabling main UI.");
    }
};

isPremium是一个布尔标志,默认为false。 updateUi方法根据isPremium状态启用/禁用按钮。

您可能还需要设置isPremium并在购买完成时致电updateUi,以确保立即启用高级功能。