如何在kotlin的app purchase中实现Android?
How to implement Android in app purchase in kotlin?
我想为我的 android 应用的应用内购买实现 Google Play 的计费,这是用 kotlin 编写的。我正在关注 this tutorial。
这是我的代码:
private lateinit var billingClient: BillingClient
private lateinit var productsAdapter: ProductsAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupBillingClient()
}
private fun setupBillingClient() {
billingClient = BillingClient
.newBuilder(this)
.setListener(this)
.build()
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(@BillingClient.BillingResponse billingResponseCode: Int) {
if (billingResponseCode == BillingClient.BillingResponse.OK) {
println("BILLING | startConnection | RESULT OK")
} else {
println("BILLING | startConnection | RESULT: $billingResponseCode")
}
}
override fun onBillingServiceDisconnected() {
println("BILLING | onBillingServiceDisconnected | DISCONNECTED")
}
})
}
fun onLoadProductsClicked(view: View) {
if (billingClient.isReady) {
val params = SkuDetailsParams
.newBuilder()
.setSkusList(skuList)
.setType(BillingClient.SkuType.INAPP)
.build()
billingClient.querySkuDetailsAsync(params) { responseCode, skuDetailsList ->
if (responseCode == BillingClient.BillingResponse.OK) {
println("querySkuDetailsAsync, responseCode: $responseCode")
initProductAdapter(skuDetailsList)
} else {
println("Can't querySkuDetailsAsync, responseCode: $responseCode")
}
}
} else {
println("Billing Client not ready")
}
}
private fun initProductAdapter(skuDetailsList: List<SkuDetails>) {
productsAdapter = ProductsAdapter(skuDetailsList) {
val billingFlowParams = BillingFlowParams
.newBuilder()
.setSkuDetails(it)
.build()
billingClient.launchBillingFlow(this, billingFlowParams)
}
//products.adapter = productsAdapter
}
override fun onPurchasesUpdated(responseCode: Int, purchases: MutableList<Purchase>?) {
println("onPurchasesUpdated: $responseCode")
allowMultiplePurchases(purchases)
}
private fun allowMultiplePurchases(purchases: MutableList<Purchase>?) {
val purchase = purchases?.first()
if (purchase != null) {
billingClient.consumeAsync(purchase.purchaseToken) { responseCode, purchaseToken ->
if (responseCode == BillingClient.BillingResponse.OK && purchaseToken != null) {
println("AllowMultiplePurchases success, responseCode: $responseCode")
} else {
println("Can't allowMultiplePurchases, responseCode: $responseCode")
}
}
}
}
private fun clearHistory() {
billingClient.queryPurchases(BillingClient.SkuType.INAPP).purchasesList
.forEach {
billingClient.consumeAsync(it.purchaseToken) { responseCode, purchaseToken ->
if (responseCode == BillingClient.BillingResponse.OK && purchaseToken != null) {
println("onPurchases Updated consumeAsync, purchases token removed: $purchaseToken")
} else {
println("onPurchases some troubles happened: $responseCode")
}
}
}
}
companion object {
private val skuList = listOf("get_5_coins", "get_10_coins")
}
我还在我的 build.gradle
中添加了 implementation 'com.android.billingclient:billing:1.2.2'
,还在我的 menifest 文件中添加了 com.android.vending.BILLING
和互联网使用权限。
当我运行此代码时,它显示计费客户端尚未准备好。
- 通常计费客户端在模拟器中不起作用。因此,要么让模拟器工作,要么更简单的方法是实际检查它 android phone,应用程序将使用测试人员的电子邮件下载。
- 非常仔细地完成google游戏设置。那部分非常有用。我在那边使用的文档实际上非常好。我一开始无法完全理解它。这都是我的错。另外,您可以检查 this documentation.
我想为我的 android 应用的应用内购买实现 Google Play 的计费,这是用 kotlin 编写的。我正在关注 this tutorial。
这是我的代码:
private lateinit var billingClient: BillingClient
private lateinit var productsAdapter: ProductsAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupBillingClient()
}
private fun setupBillingClient() {
billingClient = BillingClient
.newBuilder(this)
.setListener(this)
.build()
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(@BillingClient.BillingResponse billingResponseCode: Int) {
if (billingResponseCode == BillingClient.BillingResponse.OK) {
println("BILLING | startConnection | RESULT OK")
} else {
println("BILLING | startConnection | RESULT: $billingResponseCode")
}
}
override fun onBillingServiceDisconnected() {
println("BILLING | onBillingServiceDisconnected | DISCONNECTED")
}
})
}
fun onLoadProductsClicked(view: View) {
if (billingClient.isReady) {
val params = SkuDetailsParams
.newBuilder()
.setSkusList(skuList)
.setType(BillingClient.SkuType.INAPP)
.build()
billingClient.querySkuDetailsAsync(params) { responseCode, skuDetailsList ->
if (responseCode == BillingClient.BillingResponse.OK) {
println("querySkuDetailsAsync, responseCode: $responseCode")
initProductAdapter(skuDetailsList)
} else {
println("Can't querySkuDetailsAsync, responseCode: $responseCode")
}
}
} else {
println("Billing Client not ready")
}
}
private fun initProductAdapter(skuDetailsList: List<SkuDetails>) {
productsAdapter = ProductsAdapter(skuDetailsList) {
val billingFlowParams = BillingFlowParams
.newBuilder()
.setSkuDetails(it)
.build()
billingClient.launchBillingFlow(this, billingFlowParams)
}
//products.adapter = productsAdapter
}
override fun onPurchasesUpdated(responseCode: Int, purchases: MutableList<Purchase>?) {
println("onPurchasesUpdated: $responseCode")
allowMultiplePurchases(purchases)
}
private fun allowMultiplePurchases(purchases: MutableList<Purchase>?) {
val purchase = purchases?.first()
if (purchase != null) {
billingClient.consumeAsync(purchase.purchaseToken) { responseCode, purchaseToken ->
if (responseCode == BillingClient.BillingResponse.OK && purchaseToken != null) {
println("AllowMultiplePurchases success, responseCode: $responseCode")
} else {
println("Can't allowMultiplePurchases, responseCode: $responseCode")
}
}
}
}
private fun clearHistory() {
billingClient.queryPurchases(BillingClient.SkuType.INAPP).purchasesList
.forEach {
billingClient.consumeAsync(it.purchaseToken) { responseCode, purchaseToken ->
if (responseCode == BillingClient.BillingResponse.OK && purchaseToken != null) {
println("onPurchases Updated consumeAsync, purchases token removed: $purchaseToken")
} else {
println("onPurchases some troubles happened: $responseCode")
}
}
}
}
companion object {
private val skuList = listOf("get_5_coins", "get_10_coins")
}
我还在我的 build.gradle
中添加了 implementation 'com.android.billingclient:billing:1.2.2'
,还在我的 menifest 文件中添加了 com.android.vending.BILLING
和互联网使用权限。
当我运行此代码时,它显示计费客户端尚未准备好。
- 通常计费客户端在模拟器中不起作用。因此,要么让模拟器工作,要么更简单的方法是实际检查它 android phone,应用程序将使用测试人员的电子邮件下载。
- 非常仔细地完成google游戏设置。那部分非常有用。我在那边使用的文档实际上非常好。我一开始无法完全理解它。这都是我的错。另外,您可以检查 this documentation.