我正在我的应用中实施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
答案 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
,以确保立即启用高级功能。