Search code examples
javaandroidin-app-billingplay-billing-library

Unable to connect to Google Play billing


I am trying to simple connect to Google play billing & access details such as the title & the pricing but I've been unable to do so.

What I've done so far (step-by-step):

(Added the IAP details on Google play console as well as published the app)

  1. Added the Billing permission in the Manifest file -> <uses-permission android:name="com.android.vending.BILLING" />

  2. Added the Billing library (v.4.0) -> implementation 'com.android.billingclient:billing:4.0.0'

  3. Initialized the BillingClient in MainActivity & on trying to connect to it, I get no response from the onBillingSetupFinished callback method

    private void initializeBillingClient(Context context) {
     billingClient = BillingClient.newBuilder(context)
             .enablePendingPurchases()
             .setListener(this)
             .build();
    
     connectToGooglePlayBilling(context);
    }
    
    
    private void connectToGooglePlayBilling(Context context) {
     billingClient.startConnection(new BillingClientStateListener() {
         @Override
         public void onBillingServiceDisconnected() {
             connectToGooglePlayBilling(context);
         }
    
         @Override
         public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
             Toast.makeText(context, "connected to google play billing", Toast.LENGTH_SHORT).show();
             if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                 //I should then fetch & display premium content details
             }
         }
     });
    }
    

Weird enough I noticed that when I enable the Battery Saving Mode (I'm using a Samsung device) the onBillingSetupFinished method gets called as the toast is shown.

What am I doing incorrect & also if anyone can explain why enabling Battery save mode suddenly makes it work?


Solution

  • Make sure that onBillingSetupFinished is called in UI thread.
    Try to use runOnUiThread to show the message:

    runOnUiThread(() -> 
                    Toast.makeText(context, "connected to google play billing", Toast.LENGTH_SHORT).show());