InApp Purcahse 问题:为什么显示未解决的参考:BillingFlowParams 的 setType?

InApp Purcahse Issue : Why its showing Unresolved Reference: setType for BillingFlowParams?

在 android 中使用结算库并购买 订阅 而不是应用内商品(消耗品-非消耗品),将 .setType(SkuType.SUBS) 添加到 BillingFlowParams 如下:

val billingFlowParams = BillingFlowParams
                                    .newBuilder()
                                    .setType(SkuType.SUBS)
                                    .setSkuDetails(skuDetailsList!![i])
                                    .build()

但是,.setType(SkuType.SUBS) 显示 未解决的参考:setType

可能是什么问题? 如何启动应用内订阅的购买流程

尝试BillingClient.SkuType.SUBS
您可以启动如下购买流程

billingClient.launchBillingFlow(this, billingFlowParams).getResponseCode();

如果我们更彻底,
-顺便说一句,它在 java

首先,您必须在 class 中实现 PurchasesUpdatedListener, AcknowledgePurchaseResponseListener 并覆盖 onPurchasesUpdatedonAcknowledgePurchaseResponse 方法。
然后初始化计费客户端

private void initBilling() {
        billingClient = BillingClient.newBuilder(this)
                .enablePendingPurchases()
                .setListener(this)
                .build();
        billingClient.startConnection(new BillingClientStateListener() {

            @Override
            public void onBillingSetupFinished(BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                    Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.SUBS);
                    billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS, (billingResult1, purchaseHistoryRecordList) -> {
                        Log.i("SKURESLUT", purchaseHistoryRecordList.toString());
                    });
                }
            }

            @Override
            public void onBillingServiceDisconnected() {

            }
        });
    }

然后您可以将所需的包 ID 传递给以下方法并启动 billingFlow,

private void getBillingPackage(String id) {
        List<String> productIdsList = new ArrayList<>();
        productIdsList.add(id);
        SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
        params.setSkusList(productIdsList).setType(BillingClient.SkuType.SUBS);
        billingClient.querySkuDetailsAsync(params.build(),
                (result, skuDetailsList) -> {
                    if (!skuDetailsList.isEmpty()) {
                        mSkuDetails.addAll(skuDetailsList);
                        BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
                                .setSkuDetails(skuDetailsList.get(0))
                                .build();
                        int responseCode = billingClient.launchBillingFlow(this, billingFlowParams).getResponseCode();
                    } else {
                        Toast.makeText(this, "Requested package not available", Toast.LENGTH_LONG).show();
                    }
                    Log.e("SKURESLUT", result.toString());
                });
    }

然后在 onPurchaseUpdated 方法将在计费流程完成或取消时调用。您可以在此处确认购买

@Override
    public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
        if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
                && purchases != null) {
            for (Purchase purchase : purchases) {
                Log.e("Purchase:", purchase.toString());
                if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
                    currentPurchase = purchase;
                    if (!purchase.isAcknowledged()) {
                        AcknowledgePurchaseParams acknowledgePurchaseParams =
                                AcknowledgePurchaseParams.newBuilder()
                                        .setPurchaseToken(purchase.getPurchaseToken())
                                        .build();
                        billingClient.acknowledgePurchase(acknowledgePurchaseParams, this);
                    }
                }
            }
        } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
            Toast.makeText(this, "Purchase Cancelled", Toast.LENGTH_LONG).show();
        } else {
            // Handle any other error codes.
        }
    }

然后你可以得到如下的AcknowledgePurchase

@Override
    public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
        if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
            //Purchase Acknowledged and verified
        } else {
            Toast.makeText(this, "Purchase Failed", Toast.LENGTH_LONG).show();
        }
        Log.e("Acknowledged Purchase:", billingResult.toString());

    }