Am having trouble implementing a conditional structure for the BillingClient class method queryPurchases(String y) to check if user has an active subscription and let him download an image in the wallpaper app else prevent him from doing so. Am having trouble getting a comparison target for the if method here is the code am using

 case R.id.cv_download:
                //This is where the app's business logic goes
                //create a new BillingClient class
                 final BillingClient myclient= BillingClient.newBuilder(getActivity()).build();
                 //Establish connection to google play
                 myclient.startConnection(new BillingClientStateListener() {
                     @Override
                     public void onBillingSetupFinished(BillingResult billingResult) {
                         //The building client is ready you can setup purchases here
                         switch(billingResult.getResponseCode()){
                             case OK:
                                 myclient.queryPurchases(BillingClient.SkuType.SUBS);
                                 //I need a conditional structure here that lets me check if user has active
                                   subscription

                         }

                     }

                     @Override
                     public void onBillingServiceDisconnected() {
                    //Inform user that its not working and he should try some other time
                         Toast.makeText(getActivity(),"Connection Problems, Try Again Later",Toast.LENGTH_LONG).show();
                     }
                 });
                 //Implement conditional structure to check if user has subscription and then let him download

                 anImage      = ((BitmapDrawable) ivRandom.getDrawable()).getBitmap();
                saveImageToExternalStorage(anImage);
                Toast.makeText(getActivity(), "Download successfuly", Toast.LENGTH_SHORT).show();

                break;

Dani AI

Generated

Two practical points that tie together s code and s suggestion:

  • Do the entitlement check before you start the download and do it asynchronously. Create and reuse a single BillingClient (not a new one on every click), call the async query for subscriptions, then only call your saveImageToExternalStorage code after you confirm an active subscription. This avoids race conditions and redundant connections.

  • For security and correctness, treat the Play Store result as the first line of truth but verify server-side if the entitlement is important. The Play Billing query on the device can tell you whether Play reports an active subscription, but the authoritative fields for expiry/autoRenew status are available from the Google Play Developer API (Purchases.subscriptions.get). Use server-side validation when you must enforce access reliably (refunds, cancellations, device spoofing).

Example flow (high level)

  • Initialize a single BillingClient at app startup and keep it ready.
  • On download tap: run queryPurchasesAsync for subscriptions.
  • Inspect each Purchase: confirm it contains your SKU, has PurchaseState.PURCHASED, and (if relevant) is acknowledged. If found, enable download; otherwise block it.
  • Optionally send the purchaseToken to your backend and call the Play Developer API to confirm expiry/autoRenew status before granting long-lived access.

Notes and cautions

  • Do not cache entitlement indefinitely on the device; cache only briefly and re-check periodically or at app start.
  • If you need the absolute truth about subscription expiry, use server-side verification with the Play Developer API. Client-only checks are fine for convenience but are weaker security-wise.

Official references:

You have two operations depending on you start app approach. I mean if you always require login each time an App is started.

In that case when the user attempt to login simply return the account status in whatever way you want so that you can check the value and see if the account is active or not.

Another method is to request an account status feom your database to see if the account is active or not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.