android 中未显示应用内购买对话框

in app purchase dialog not showing in android

我已经实现了应用内购买。问题是计费对话框 billingClient.launchBillingFlow 未在某些设备中显示。首先,我在 android 8 中进行了测试,一切正常,但是当我在 android 9 中进行测试时,计费对话框未显示。 这是代码

billingClient = BillingClient.newBuilder(this)
                .enablePendingPurchases()
                .setListener(this).build();

        billingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {

                    List<String> skuList = new ArrayList<>();
                    skuList.add(ITEM_SKU_diamond_500);
                    final SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
                    params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
                    billingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
                        @Override
                        public void onSkuDetailsResponse(@NonNull BillingResult billingResult, List<SkuDetails> skuDetailsList) {
                            if (skuDetailsList != null && billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                                for (SkuDetails skuDetails : skuDetailsList) {
                                    String sku = skuDetails.getSku();
                                    String price = skuDetails.getPrice();

                                    final BillingFlowParams params = BillingFlowParams.newBuilder()
                                            .setSkuDetails(skuDetails)
                                            .build();

                                    if (ITEM_SKU_diamond_500.equals(sku)) {
                                        premiumUpgradePrice = price;
                                        firstBtn500(params);

                                    }

                                    //clickHandler(skuDetails, sku);
                                }
                            } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.ERROR) {
                                Toast.makeText(DiamondsActivity.this, "Error", Toast.LENGTH_SHORT).show();
                            }
                        }

                    });

                } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.SERVICE_TIMEOUT) {
                    Toast.makeText(DiamondsActivity.this, "Service timeout", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(DiamondsActivity.this, "Failed to connect to the billing client", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onBillingServiceDisconnected() {
                Toast.makeText(DiamondsActivity.this, "Disconnected from the client", Toast.LENGTH_SHORT).show();
            }
        });
 private void firstBtn500(final BillingFlowParams params) {

        firstPurchaseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(DiamondsActivity.this, "OK: 1", Toast.LENGTH_SHORT).show();

//The toast message is showing.
// but the billing dialog is not showing

                billingClient.launchBillingFlow(DiamondsActivity.this, params);

            }
        });

    }

 @Override
    public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List<Purchase> purchases) {

        if (purchases != null && billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {

            for (Purchase purchase : purchases) {
                handlePurchases(purchase);
            }

        } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
            Toast.makeText(this, "Purchased Canceled", Toast.LENGTH_SHORT).show();
        }

    }
private void handlePurchases(final Purchase purchase) {

        if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
           
            // Todo :Consume the purchase async
            ConsumeParams consumeParams = ConsumeParams.newBuilder()
                    .setPurchaseToken(purchase.getPurchaseToken())
                    .build();

            ConsumeResponseListener consumeResponseListener = new ConsumeResponseListener() {
                @Override
                public void onConsumeResponse(BillingResult billingResult, @NonNull String purchaseToken) {

                    Toast.makeText(DiamondsActivity.this, "Purchase successful", Toast.LENGTH_SHORT).show();

                    if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {

                        if (purchase.getSku().equalsIgnoreCase(ITEM_SKU_diamond_500)) {
                            Toast.makeText(DiamondsActivity.this, "Thank you for purchasing!", Toast.LENGTH_SHORT).show();
                            handleAdditionOfData(500);
                        } 

                    }

                }
            };

            billingClient.consumeAsync(consumeParams, consumeResponseListener);


        } else if (purchase.getPurchaseState() == Purchase.PurchaseState.PENDING) {

            Toast.makeText(this, "Purchase pending", Toast.LENGTH_SHORT).show();

        }

    }

用这个方法更新它:

 private void firstBtn500(final BillingFlowParams params) {
    
            firstPurchaseBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(DiamondsActivity.this, "OK: 1", Toast.LENGTH_SHORT).show();
    
       BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
                                  .setSkuDetails(skuDetails)
                                  .build();
     int responseCode = billingClient.launchBillingFlow(activity, params);
    
                   
    
                }
            });
    
        }