根据 Google 的教程,我使用 Play Billing Library 1.0 实现了应用内购买。我只有一件要购买的物品,当它被解锁时,我会显示一条长度为 Toast.LENGTH_SHORT 的 Toast 消息。但是,Toast 会在那里停留大约 10 秒,所以我假设它会被多次调用。当我通过 queryPurchases 解锁它时不会发生这种情况(如果有人早些时候购买了它并在此期间重新安装了应用程序)。
任何人都知道为什么 Toast 会停留这么久/为什么会被多次调用?
在我的 BillingManager 类中:
@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
if (responseCode == BillingClient.BillingResponse.OK) {
for (Purchase purchase : purchases) {
handlePurchases(purchase);
}
mBillingUpdatesListener.onPurchasesUpdated(mPurchases);
} else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
} else {
}
}
public void handlePurchases(Purchase purchase) {
//here could be validation on own server
mPurchases.add(purchase);
}
Main Activity 实现 BillingUpdatesListener:
@Override
public void onPurchasesUpdated(List<Purchase> purchases) {
for (Purchase purchase : purchases) {
switch (purchase.getSku()) {
case "premium":
unlockPremium();
break;
}
}
}
public void unlockPremium() {
mPremiumUnlocked = true;
savePremiumUnlocked();
Toast.makeText(this, getResources().getString(R.string.premium_congrats), Toast.LENGTH_SHORT).show();
mAdView.setVisibility(GONE);
}