android 计费库5 QueryPurchasesAsync始终返回服务断开

ylamdve6  于 2023-03-16  发布在  Android
关注(0)|答案(2)|浏览(307)

从计费库4.0迁移到5.0时,我无法检查购买状态。

billingClient.queryPurchasesAsync(QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.INAPP).build(), new PurchasesResponseListener() {

        @Override
        public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List<Purchase> list) {
            if(billingResult.getResponseCode() == RESULT_OK && list != null){



               Toast.makeText(getApplicationContext(),"purchase is done", Toast.LENGTH_LONG).show();

            }
            else if(billingResult.getResponseCode() == RESULT_CANCELED && list == null){

                Toast.makeText(getApplicationContext(),"purchase not done", Toast.LENGTH_LONG).show();
            }
        }
    });
wlp8pajw

wlp8pajw1#

调用queryPurchasesAsync返回以下响应(转换为字符串表示)

{"orderId":"GPA.3376-0182","packageName":"yourPackage","productId":"yourProductId","purchaseTime":1666868818195,"purchaseState":0,"purchaseToken":"kpomfmjcbkbdpnocfhoplnbm.AO-J1OwRcn0dSHxAYs0611","quantity":1,"autoRenewing":true,"acknowledged":true}

这里的键purchaseState告诉您的购买状态。您可以从下面的链接here参考更多关于购买状态的详细信息

0x6upsns

0x6upsns2#

您的billingClient未初始化。

billingClient!!.startConnection(object : BillingClientStateListener {
        override fun onBillingSetupFinished(billingResult: BillingResult) {

            if (billingResult.responseCode ==  BillingClient.BillingResponseCode.OK) {
                val params = QueryPurchasesParams.newBuilder()
                    .setProductType(ProductType.INAPP)

                val purchasesResult = billingClient!!.queryPurchasesAsync(params.build(), object : PurchasesResponseListener {
                    override fun onQueryPurchasesResponse(p0: BillingResult, p1: List<Purchase>) {
                    }
                } )


            }
        }

        override fun onBillingServiceDisconnected() {

        }
    })

相关问题