我可以在我的代码中管理以执行应用内结算。但是,如果我安装应用程序并重做购买,则会再次购买。
问题出在哪里?
以下是代码:
case R.id.action_disable_ads:
try {
mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
mPurchaseFinishedListener);
} catch (IabHelper.IabAsyncInProgressException e) {
//e.printStackTrace();
}
return true;
}
return super.onOptionsItemSelected(item);
}
//functions special for in App Billing
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data)
{
if (!mHelper.handleActivityResult(requestCode,
resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
= new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result,
Purchase purchase)
{
if (result.isFailure()) {
// Handle error
return;
}
else if (purchase.getSku().equals(ITEM_SKU)) {
consumeItem();
//buyButton.setEnabled(false);
}
}
};
public void consumeItem() {
try {
mHelper.queryInventoryAsync(mReceivedInventoryListener);
} catch (IabHelper.IabAsyncInProgressException e) {
//e.printStackTrace();
}
}
IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
= new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
if (result.isFailure()) {
// Handle failure
} else {
try {
mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
mConsumeFinishedListener);
} catch (IabHelper.IabAsyncInProgressException e) {
//e.printStackTrace();
}
}
}
};
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase,
IabResult result) {
if (result.isSuccess()) {
//first save purchase state
SharedPreferences settings = getPreferences(0);
SharedPreferences.Editor settingsEditor = settings.edit();
settingsEditor.putString("adfree", "purchased");
settingsEditor.apply();
//then, hide the disable ads button
MenuItem disableAds = mainMenu.findItem(R.id.action_disable_ads);
disableAds.setVisible(false);
//then display a message that the transaction is successful
Toast.makeText(getApplicationContext(), "AdFree bundle purchased successfully!", Toast.LENGTH_SHORT).show();
//then change the state of ad-free bundle for current session:
adFreeBundle = "purchased";
//then disable ads
interstitialAd = null;
mAdView.setVisibility(View.GONE);
} else {
//display an error message that the transaction was not successful
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("TRANSACTION FAILED!");
builder.setMessage("Unfortunately, the transaction was unsuccessful!");
builder.setPositiveButton("CLOSE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
dialog.dismiss();
}
});
final AlertDialog alert = builder.create();
alert.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface arg0) {
alert.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.parseColor("#000000"));
}
});
alert.show();
}
}
};
@Override
public void onDestroy() {
super.onDestroy();
if (mHelper != null) try {
mHelper.dispose();
} catch (IabHelper.IabAsyncInProgressException e) {
//e.printStackTrace();
}
mHelper = null;
}
应用内结算的过程运作得很好。但是,我很乐意在重新安装应用程序时尝试获取所购买的项目。
答案 0 :(得分:0)
这是因为您正在进行购买,并在sharedPreferences中设置了一个标志。
不要这样做任何人都可以使用正确的工具更改sharedPreferences。
当您卸载应用时,您的sharedPreferences将会重置,您将失去“购买”。
因为您使用该物品,所以在第二次尝试时,您的库存将无法使用正确的SKU,这就是您再次购买的原因。
如果您始终可以看到购买按钮,则会要求您支付每次点击费用。
要解决此问题
不要使用该项,最重要的是不要在sharedPreferences中设置任何标志。这也适用于sql数据库。
消费不是用于非消费品,这是你的情况,因为你向用户收取删除广告的费用,如果你试图消费它,你的用户就不会很开心
当您的应用开始时,如果您的广告资源具有正确的SKU,请检查您的广告资源,然后向用户提供该产品。
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
@Override
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
return;
}
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null) return;
// IAB is fully set up. Now, let's get an inventory of stuff we own.
Log.d(TAG, "Setup successful. Querying inventory.");
try {
mHelper.queryInventoryAsync(new IabHelper.QueryInventoryFinishedListener() {
@Override
public void onQueryInventoryFinished(IabResult result, Inventory inv) {
handleInventory(result, inv);
}
});
} catch (IabHelper.IabAsyncInProgressException e) {
complain("Error querying inventory. Another async operation in progress.");
e.printStackTrace();
}
}
});
public void handleInventory(IabResult result, Inventory inventory) {
if (mHelper == null) return;
// Is it a failure?
if (result.isFailure()) {
return;
}
Log.d(TAG, "Query inventory was successful.");
Purchase premiumPurchase = inventory.getPurchase(YOUR SKU);
//verify Developer Payload here, it's recommended but not mandatory.
//mIsPremium = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase));
mIsPremium = premiumPurchase != null;
if (mIsPremium) {
//Enable feature
}else{
//Disable feature
}
}