簡體   English   中英

無法使用com.android.billingclient:billing獲得SkuDetail

[英]cannot get SkuDetail using com.android.billingclient:billing

我正在為Google Play開發一款游戲。現在該進行應用內購買了,我按照doc進行操作 ,但是我無法從代碼中獲取SkuDetail 我使用庫com.android.billingclient:billing:1.0


    public void queryProductList(String product) {
        List<String> skuList = new ArrayList<>();
        skuList.add(product);

        SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
        params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
        billingClient.querySkuDetailsAsync(params.build(),
                new SkuDetailsResponseListener() {
                    @Override
                    public void onSkuDetailsResponse(int billingResult,
                                                     List<SkuDetails> skuDetailsList) {

                        if (billingResult == 0) {

                            Log.i(TAG, "getResponseCode ok");

                            if (skuDetailsList == null) {
                                Log.i(TAG, "skuDetailsList null");
                            } else {
                                Log.i(TAG, "skuDetailsList not null:" + skuDetailsList.size());// size is always 0
                            }

                        }

                    }
                });
    }

這是我為SkuDetail附加代碼的項目代碼。 您可以進行一些編輯和使用。

 public void startServiceForConnectInBilling(String billing_period_of_product_1) {
            this.product1 = billing_period_of_product_1;

            myBillingClient.startConnection(new BillingClientStateListener() {
                @Override
                public void onBillingSetupFinished(BillingResult billingResult) {
                    try {
                        if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                            isServiceConnected = true;
                            if (areSubscriptionsSupported()) {
                                ArrayList<String> arrayList = new ArrayList<>();
                                arrayList.add(billing_period_of_product_1);
                                SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
                                params.setSkusList(arrayList).setType(BillingClient.SkuType.SUBS);
                                Runnable queryRequest = () -> myBillingClient.querySkuDetailsAsync(params.build(), (billingResult1, skuDetailsList) -> {
                                    if (billingResult1.getResponseCode() != BillingClient.BillingResponseCode.OK) {
                                        if (BuildConfig.DEBUG)
                                            Log.e(TAG, "Error billing:" + billingResult1.getDebugMessage());
                                    } else if (skuDetailsList != null && skuDetailsList.size() > 0) {
                                        skuResultList.addAll(skuDetailsList);
                                        if (SharedPreferenceHelper.getSharedPreferenceBoolean(context, AppConstants.GOOGLE_ON_OFF, false))
                                            checkCondition();
                                    }
                                });
                                executeServiceRequest(queryRequest);
                            }
                        }
                    } catch (Exception e) {
                        e.getMessage();
                    }
                }

                @Override
                public void onBillingServiceDisconnected() {

                }
            });
        }

        private void executeServiceRequest(Runnable runnable) {
            if (isServiceConnected) {
                runnable.run();
            } else {
                // If billing service was disconnected, we try to reconnect 1 time.
                // (feel free to introduce your retry policy here).
                startServiceForConnectInBilling(product1);
            }
        }
      private boolean areSubscriptionsSupported() {
            if (myBillingClient == null) {
                if (BuildConfig.DEBUG)
                    Log.e(TAG, "Billing client was null and quitting");
                return false;
            }
            BillingResult responseCode = myBillingClient.isFeatureSupported(BillingClient.FeatureType.SUBSCRIPTIONS);
            if (responseCode.getResponseCode() != BillingClient.BillingResponseCode.OK) {
                if (BuildConfig.DEBUG)
                    Log.e(TAG,
                            "areSubscriptionsSupported() got an error response: " + responseCode);
            }
            return responseCode.getResponseCode() == BillingClient.BillingResponseCode.OK;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM