【发布时间】:2020-08-05 08:12:22
【问题描述】:
我在 Google 商店(在 Kotlin 中)有一些带有 AdMob 的应用,现在我想添加应用内购买来删除广告。 我一直在寻找一些好的教程,但没有结果,因为它们都太老了(2016-2017)。
谁能指导我,如何实现这个? 我知道我可以使用 SharedPreferences,但如果有人重新安装我的应用程序或删除内存,他将丢失购买的项目。
感谢大家的帮助!
@编辑 购买在我的 MainActivity 中,用户应该可以在点击按钮后购买 Remove Ads,其 id 为“noAdsButton”,我的产品在 Google Play 中的 id 为“no_ads”
private lateinit var mBillingClient: BillingClient
...
open fun onPurchasesUpdated(
billingResult: BillingResult,
list: List<Purchase>?
): Unit {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK
&& list != null
) {
for (purchase in list) {
if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
if (!purchase.isAcknowledged) {
val acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.purchaseToken)
.build()
mBillingClient.acknowledgePurchase(
acknowledgePurchaseParams,
AcknowledgePurchaseResponseListener { billingResult ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
Toast.makeText(
this@MainActivity,
"Ads Removed",
Toast.LENGTH_SHORT
).show()
preferences.edit().putBoolean("show_ads", false).apply()
startActivity(
Intent(
this@MainActivity,
MainActivity::class.java
)
)
finish()
} else {
Toast.makeText(
this@MainActivity,
"Error",
Toast.LENGTH_LONG
).show()
}
})
}
}
}
} else if (billingResult.responseCode == BillingClient.BillingResponseCode.USER_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show()
} else if (billingResult.responseCode == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) {
preferences.edit().putBoolean("show_ads", false).apply()
Toast.makeText(this, "Ads removed because you buy before", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show()
}
}
当我想开始购买时,我应该在按钮上设置 onClickListener 并启动函数 onPurchasesUpdated("what here?")
【问题讨论】:
标签: android kotlin in-app-purchase ads