如何在 billingClient.queryProductDetailsAsync 方法中的所有按钮上设置购买价格

问题描述 投票:0回答:1

所以,我尝试在我的所有按钮上设置价格购买。我只能为我的一个按钮设置价格,但我不知道为什么它不适用于我的其他按钮。虽然我也做同样的事情。

我的购买对话框:

public class PurchaseDialog extends DialogFragment {

    private View view;
    private AppCompatButton oneWeekPurchaseButton, oneMonthPurchaseButton, oneYearPurchaseButton;
    private BillingClient billingClient;
    private List<ProductDetails> productDetails;
    private Purchase purchase;

    static final String TAG = "InAppPurchaseTag";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        view = inflater.inflate(R.layout.purchase_dialog_fragment, container, false);

        oneWeekPurchaseButton = view.findViewById(R.id.oneWeekPurchaseButton);
        oneMonthPurchaseButton = view.findViewById(R.id.oneMonthPurchaseButton);
        oneYearPurchaseButton = view.findViewById(R.id.oneYearPurchaseButton);

        billingSetup();

        return view;
    }

private void billingSetup() {

    PurchasesUpdatedListener purchasesUpdatedListener = new PurchasesUpdatedListener() {
        @Override
        public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
            if (billingResult.getResponseCode() ==
                    BillingClient.BillingResponseCode.OK
                    && purchases != null) {
                for (Purchase purchase : purchases) {
                    // completePurchase(purchase);
                }
            } else if (billingResult.getResponseCode() ==
                    BillingClient.BillingResponseCode.USER_CANCELED) {
                Log.i(TAG, "onPurchasesUpdated: Purchase Canceled");
            } else {
                Log.i(TAG, "onPurchasesUpdated: Error");
            }
        }
    };

    billingClient = BillingClient.newBuilder(getContext())
            .setListener(purchasesUpdatedListener)
            .enablePendingPurchases()
            .build();

    billingClient.startConnection(new BillingClientStateListener() {

        @Override
        public void onBillingSetupFinished(
                @NonNull BillingResult billingResult) {

            if (billingResult.getResponseCode() ==
                    BillingClient.BillingResponseCode.OK) {
                Log.i(TAG, "OnBillingSetupFinish connected");
                queryProduct();
            } else {
                Log.i(TAG, "OnBillingSetupFinish failed");
            }
        }

        @Override
        public void onBillingServiceDisconnected() {
            Log.i(TAG, "OnBillingSetupFinish connection lost");
        }
    });
}

private void queryProduct() {

    ImmutableList<QueryProductDetailsParams.Product> productList = ImmutableList.of(
            //Product 1
            QueryProductDetailsParams.Product.newBuilder()
                    .setProductId("premium_subs_test")
                    .setProductType(BillingClient.ProductType.SUBS)
                    .build(),

            //Product 2
            QueryProductDetailsParams.Product.newBuilder()
                    .setProductId("one_month_premium_test")
                    .setProductType(BillingClient.ProductType.SUBS)
                    .build(),

            //Product 3
            QueryProductDetailsParams.Product.newBuilder()
                    .setProductId("one_year_premuim_test")
                    .setProductType(BillingClient.ProductType.SUBS)
                    .build()
    );


    QueryProductDetailsParams queryProductDetailsParams = QueryProductDetailsParams.newBuilder()
            .setProductList(productList)
            .build();

    billingClient.queryProductDetailsAsync(
            queryProductDetailsParams,
            new ProductDetailsResponseListener() {
                public void onProductDetailsResponse(
                        @NonNull BillingResult billingResult,
                        @NonNull List<ProductDetails> productDetailsList) {
                    productDetails = productDetailsList;
                    oneWeekPurchaseButton.setText(productDetails.get(0).getSubscriptionOfferDetails()
                            .get(0).getPricingPhases().getPricingPhaseList().get(0).getFormattedPrice());
                    oneMonthPurchaseButton.setText(productDetails.get(0).getSubscriptionOfferDetails()
                            .get(0).getPricingPhases().getPricingPhaseList().get(0).getFormattedPrice());
                    oneYearPurchaseButton.setText(productDetails.get(0).getSubscriptionOfferDetails()
                            .get(0).getPricingPhases().getPricingPhaseList().get(0).getFormattedPrice());
                }
            }
    );

}

当我查看调试器时,我看到在

queryProduct()
中,在
billingClient.queryProductDetailsAsync
方法中,我的代码没有执行到底。

这是代码执行的最后两行:

                productDetails = productDetailsList;
                oneWeekPurchaseButton.setText(productDetails.get(0).getSubscriptionOfferDetails()
                        .get(0).getPricingPhases().getPricingPhaseList().get(0).getFormattedPrice());

很清楚为什么其余按钮中没有设置订阅价格。但我不知道为什么会发生这种情况。

我无法填写为什么所有代码都没有执行?怎么解决这个问题?

java in-app-purchase in-app-billing
1个回答
0
投票

我解决了这个问题。我用

runOnUiThread(()

             getActivity().runOnUiThread(() -> {
                    oneWeekPurchaseButton.setText(productDetails.get(2).getSubscriptionOfferDetails()
                            .get(0).getPricingPhases().getPricingPhaseList().get(0).getFormattedPrice());
                    oneMonthPurchaseButton.setText(productDetails.get(1).getSubscriptionOfferDetails()
                            .get(0).getPricingPhases().getPricingPhaseList().get(0).getFormattedPrice());
                    oneYearPurchaseButton.setText(productDetails.get(0).getSubscriptionOfferDetails()
                            .get(0).getPricingPhases().getPricingPhaseList().get(0).getFormattedPrice());
                });
© www.soinside.com 2019 - 2024. All rights reserved.