我正在将成员变量初始化为 null。
稍后在此过程中,在开始支付流程之前,即在调用
billingClient.launchBillingFlow(context, billingFlowParams)
PurchasesUpdatedListener
回调时,成员变量再次为null。
我首先认为这是一个范围问题,所以我做了一个
inner class MyPurchasesUpdatedListener : PurchasesUpdatedListener
来确保包含类成员变量在包含类中是可访问的,但这没有什么区别。
所以我认为这是完全不同的东西,比如生命周期,我自己的流程中间的计费流程隐含的不同线程/活动......
我尝试将代码剥离到其中最小的有用部分:
class GooglePlayPay(val context:NativeAppActivity) {
...
var purchaseCallback: ((Boolean)->Unit)?=null
...
private val purchasesUpdatedListener = MyPurchasesUpdatedListener()
inner class MyPurchasesUpdatedListener : PurchasesUpdatedListener {
override fun onPurchasesUpdated(billingResult:BillingResult, purchases:List<Purchase>?){
Log.d("fp_purchUpdatedListener","PurchaseCallback: ${purchaseCallback.toString()}")
// ===> in catlog: PurchaseCallback: null
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
for (purchase in purchases) {
handlePurchase(purchase)
}
} else if (billingResult.responseCode == BillingClient.BillingResponseCode.USER_CANCELED) {
// Handle an error caused by a user cancelling the purchase flow.
} else {
// Handle any other error codes.
}
}
}
fun handlePurchase(purchase: Purchase){
...
Log.d("fp_handlePurchase","PurchaseCallback: ${purchaseCallback.toString()}")
// ===> in catlog: PurchaseCallback: null
...
}
fun purchaseProduct(productDetails: ProductDetails, cb:(Boolean)->Unit) {
purchaseCallback = cb
Log.d("fp_purchaseProduct","2.PurchaseCallback: ${purchaseCallback.toString()}")
// ===> in catlog: PurchaseCallback: Function1<java.lang.Boolean, kotlin.Unit>
// Launch the billing flow
billingClient.launchBillingFlow(context, billingFlowParams)
}
我不知道这是否重要:这个
purchaseProduct()
是从 @JavascriptInterface
函数中调用的。
似乎你无法从 JavaScript 中保留对象。不要将回调保存到本地变量,而是尝试使用购买令牌在 Web 视图中加载目标页面。
fun handlePurchase(purchase: Purchase){
...
Log.d("fp_handlePurchase","PurchaseCallback: ${purchaseCallback.toString()}")
// ===> in catlog: PurchaseCallback: null
...
webView.loadUrl("my_result_page?purchase_token=...")
}