我已经实现了Billing库,以便在我的应用中包含应用内商品。我可以成功购买,一切正常。如果我再次尝试购买,则会收到“产品已拥有”的响应,表明购买是可以的。我正在使用作为测试ID的productID:“ android.test.purchased”
此外,在Main的Activity onResume()方法中,我调用queryPurchases()方法来还原产品,以防用户删除该应用并重新安装该应用。但是,尽管响应代码还可以,但是此方法返回一个空的采购清单。
这是我使用方法的方式:
private fun startServiceConnectionIfNeeded(executeOnSuccess: Runnable?) {
if (mBillingClient.isReady) {
executeOnSuccess?.run()
} else {
mBillingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(@BillingClient.BillingResponse billingResponse: Int) {
if (billingResponse == BillingClient.BillingResponse.OK) {
Log.i(TAG, "onBillingSetupFinished() response: $billingResponse")
executeOnSuccess?.run()
} else {
Log.w(TAG, "onBillingSetupFinished() error code: $billingResponse")
}
}
override fun onBillingServiceDisconnected() {
Log.w(TAG, "onBillingServiceDisconnected()")
}
})
}
}
// I call this method in Main's onResume()
fun updateBillingSharedPreferencesKey() {
val executeOnConnectedService = Runnable {
val purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.INAPP)
val purchasesList = purchasesResult?.purchasesList
purchasesList?.let {
// this list is always empty, although I have a valid product
for (purchase in it) {
// I never enter this loop to handle the product functionality
Log.d(TAG, "purchase.sku = ${purchase?.sku}")
}
}
}
startServiceConnectionIfNeeded(executeOnConnectedService)
}
我做错什么了吗?服务连接和响应代码正常。我应该如何调用queryPurchases()方法来获取有效的应用内商品?
我也在解决这个问题。我怀疑只有静态测试产品(例如“ android.test.purchased”)才可以,我将重试Beta版APK版本并在此处发布更新