Search code examples
androidin-app-purchasegoogle-play-servicesin-app-billinggoogle-play-console

Not connecting with onBillingSetupFinished method


I'm trying to show Toast inside onBillingSetupFinished method but it's never connect with it.

Code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityMainBinding activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(activityMainBinding.getRoot());

    BillingClient billingClient = BillingClient.newBuilder(this).enablePendingPurchases().setListener(this).build();
    billingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingServiceDisconnected() {
            Toast.makeText(MainActivity.this, "A", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
            Toast.makeText(MainActivity.this, "B", Toast.LENGTH_SHORT).show();
        }
    });

}

@Override
public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List<Purchase> list) {
    Toast.makeText(this, "C", Toast.LENGTH_SHORT).show();
}

Permissions

<uses-permission android:name="android.permission.INTERNET"/>

I uploaded apk to Google Play Store and they accepted it but code still not working.


Solution

  • Your code works fine. It's just that you are unable to get the toast. There are two things you can do:

    Replace the Toast with

    Log.d("GoogleBillingService", "onBillingSetupFinished: billingResult: Yes/Your Result");
    

    And check the LogCat at GoogleBillingService to get the results.

    The toast is probably running on a different thread and hence you don't see it when you run the application.

    Try using

    ((Activity)context).runOnUiThread(new Runnable()
        {
            public void run()
            { 
                 Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
            }
        });
    

    Hope this helps you out!