首页 > 解决方案 > queryPurchases() 返回一个空列表

问题描述

我已经实现了计费库,以便在我的应用程序中拥有应用程序内产品。我可以成功购买,一切正常。如果我再次尝试购买,我会收到“产品已拥有”的响应,表明购买正常。我将测试 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() 方法来获取我的有效应用内产品?

标签: androidin-app-billing

解决方案


推荐阅读