I'm initializing a member variable to null.
Later in the process, this member variable is assigned a value, before starting the payment flow, ie before invoking billingClient.launchBillingFlow(context, billingFlowParams)
When arriving in the PurchasesUpdatedListener callback, the member variable is null again.

我首先认为这是一个作用域问题,所以我设置了一个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函数中调用的.

[编辑]此处添加了调用类的摘录:

    class GooglePlayPayJSInterface(val context: WebAppActivity) {

        @JavascriptInterface fun getProduct(str:String) {
            GooglePlayPay(context).getProduct(str, ::getProductCallback)
        }
        fun getProductCallback(details: ProductDetails){
            context.postToJs("""{"data":{"productId":"${details.getProductId()}", "name": "${details.getName()}", "price":"${details.getOneTimePurchaseOfferDetails()?.getFormattedPrice()?:"n/a"}"}}""")
        }


        @JavascriptInterface fun purchase(str:String) {
            GooglePlayPay(context).purchase(str, ::purchaseCallback} )
        }
        fun purchaseCallback(success:Boolean){
            if(success){ context.postToJs("""{"data":{"result":"success"}}""") }
            else { context.postToJs("""{"errcode":"Purchase failed"}""") }
        }

        ...
        
    }

But I don't think there's a problem here: getProduct() and its getProductCallback() work like a charm.
The only difference I see between getProduct+callback and purchase+callback is that:

  • purchase被Android的计费流程中断,
  • 其中getProduct只是调用API函数(billingClient.startConnection()billingClient.queryProductDetailsAsync()),然后调用先前存储为成员变量的回调.

推荐答案

UPDATE

请try 重构您的代码,以便创建GooglePlayPay对象并将其存储为GooglePlayPayJSInterface中的字段,并在GooglePlayPayJSInterface中使用该字段.

ORIGINAL ANSWER

看起来你不能将对象从Java脚本中保留下来.不是将回调保存到本地变量,而是try 使用购买令牌在Web视图中加载目标页面.

fun handlePurchase(purchase: Purchase){
            ...
            Log.d("fp_handlePurchase","PurchaseCallback: ${purchaseCallback.toString()}")
            // ===> in catlog: PurchaseCallback: null
            ...

            webView.loadUrl("my_result_page?purchase_token=...")
        }

Android相关问答推荐

编写Inbox需要具有匹配兼容性的Kotlin版本

如何使用视图模型触发可变状态?

try 用Jetpack Compose理解视图模型和导航

Android手柄注射周期错误,多个模块引用一个核心模块

如何在Android中将以前的数据保存在复选框中?

使用 Jetpack Compose 在 Android TV 上启用系统声音

Jetpack Compose 绘制范围内的动画

在 Android Studio 中获取更新版本的 Picasso 库的错误警告消息

在 compose android 中创建一个圆形按钮和居中文本

从 Jetpack Compose 中的 IconButton 中删除黑色色调

视觉转换后获取文本

如何使用 Jetpack Compose 在图像上叠加文本

如何只允许拖动 BottomSheetScaffold 中 BottomContent 的 SheetPeek 的一部分?

在 Jetpack Compose 中清除列表时可组合不重组

Material Design 网站的新设计已启动,但我找不到实现选项卡,该选项卡在哪里?

在 Kotlin 客户端应用程序中发送 FCM 推送通知 - Firebase 云消息传递

如何在 Jetpack compose 中将 Canvas 中的文本居中?

如何使用文件提供程序将视频从一个应用程序共享到另一个应用程序?

Google Play 服务登录在 Unity Android 上无法正常运行

lambda 函数中的类型不匹配 - Kotlin