无法使用 com.android.billingclient:billing 获取 SkuDetail

Cannot get SkuDetail using com.android.billingclient:billing

我正在为 Google 开发游戏 Play.And 是时候进行应用内购买了,我遵循 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;
        }