在使用支付功能之前,首先要连接Google Play Service,确保当前状态支付是可用的,此操作对于国内用户来说是需要梯子的,但是对于国外的用户,就不用操心了,毕竟我们开发Google Play主要还是给国外用户使用。
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(@BillingClient.BillingResponse billingResponseCode: Int) {
// 连接成功
if (billingResponseCode == BillingClient.BillingResponse.OK) {
// The billing client is ready. You can query purchases here.
// 5. Query for in-app product details.
// 5. 查询商品详情
// 6. 支付商品
} else {
// TODO 连接失败
}
}
// 连接断开
override fun onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
}
})