2

I was wondering could you help. I followed the instructions at https://developer.android.com/google/play/billing/integrate, but I cannot seem to get the purchase flow working. The billing seems to setup ok, but when I try to query for my in-app products, the list is always returning empty. Can someone please help?

In my app level build.gradle file, I have included the Google Billing SDK:

implementation 'com.android.billingclient:billing:3.0.0'

Then I have created an activity to test out the code. It first initialises the BillingClient and starts the connection. The connection seems to finish the setup correctly. Once setup correctly, I then try to query the products that I have available in my Google Play Console under 'Store presence' > 'In-app products' > 'Manage products'

enter image description here

The following is then the code in the Activity that should kick off the process and return the SkuDetails list, but unfortunately it is returning back empty.

private BillingClient billingClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_billing);

    this.billingClient = BillingClient.newBuilder(this)
            .enablePendingPurchases()
            .setListener(this.purchaseUpdateListener)
            .build();
    this.billingClient.startConnection(billingClientStateListener);
}

private PurchasesUpdatedListener purchaseUpdateListener = new PurchasesUpdatedListener() {
    @Override
    public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List<Purchase> list) {
        Log.d("Billing", "onPurchasesUpdated - List Size: " + list.size());
    }
};

private BillingClientStateListener billingClientStateListener = new BillingClientStateListener() {
    @Override
    public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
        if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
            Log.d("Billing", "onBillingSetupFinished - OK");
            queryProducts();
        } else {
            Log.d("Billing", "onBillingSetupFinished - Something wrong response Code: " + billingResult.getResponseCode());
        }
    }

    @Override
    public void onBillingServiceDisconnected() {
        Log.d("Billing", "Service disconnected");
    }
};

private void queryProducts() {
    List<String> productIdsList = new ArrayList<>();
    productIdsList.add("test.billing.001");

    SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
    params.setSkusList(productIdsList).setType(BillingClient.SkuType.INAPP);
    this.billingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
        @Override
        public void onSkuDetailsResponse(@NonNull BillingResult billingResult, @Nullable List< SkuDetails > list) {
            Log.d("Billing", "onSkuDetailsResponse - List Size: " + list.size());
        }
    });
}
4

1 回答 1

1

因此,对于任何遇到类似问题的人来说,似乎(在我的情况下)我的应用程序需要成功发布,然后我才能从应用程序中检索应用内产品。一旦我的应用发布,我就可以查询和使用应用内产品。

于 2020-07-20T09:00:44.280 回答