Search code examples
androidin-app-billingandroid-billingserviceconnectiononserviceconnected

Android in-app billing: onServiceConnected is never called and bindService returns "false"


I'm currently try to add in-app billing to my app so that users can make a small donations. I use the latest version of Android Studio for development and am following this guide (step by step and I am doing every exactly like mentioned... at least I think I do :-) ): https://developer.android.com/google/play/billing/billing_integrate.html

AIDL file is placed in the mentioned location (under src/main in in package com.android.vending.billing) and I see it is being generated under the gen folder.

When I tested the retrieval of products I noticed that the method onServiceConnected is never called, it is implemented in the activity like this:

IInAppBillingService mService;

ServiceConnection mServiceConn = new ServiceConnection() {
   @Override
   public void onServiceDisconnected(ComponentName name) {
       mService = null;
   }

   @Override
   public void onServiceConnected(ComponentName name,
      IBinder service) {
       mService = IInAppBillingService.Stub.asInterface(service);
   }
};

The binding to the service is made like this (in the same activity):

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_donation);
  Intent serviceIntent =
      new Intent("com.android.vending.billing.InAppBillingService.BIND");
  serviceIntent.setPackage("com.android.vending");
  bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
}

I noticed two things:

  • when I leave the donation activity I see an exception in the console: ... has leaked ServiceConnection ... that was originally bound here - I see some people suggest to replace use bindService on the application context on not the activity and indeed if I do this the issue is gone. But I think that is unrelated to my main issue with the onServiceConnected never called and also, why is it in the official guide called on the activity?
  • bindService returns a boolean and I checked the return value, it was false. So I guess this is where the issue comes from.

I saw that some people say that an entry for the service must be added to the AndroidManifest.xml - like this:

Tried this, but it made no difference and also this entry is not mentioned in the official guide, so I believe it is not needed.

Any ideas what I am doing wrong?


Solution

  • Actually I found out what was "wrong" in the meantime.

    According to the official Testing In-app Billing guide (https://developer.android.com/google/play/billing/billing_testing.html):

    Install your application on an Android-powered device.

    You cannot use the emulator to test In-app Billing; you must install your application on a device to test In-app Billing.

    Unfortunately this information was not in the other guide and I normally test everything on the emulator first. After deploying the app to a real device and testing it there it was clear that there was no issue.

    I now included a message that appears when bindService returns false that informs the user that the connection to the service could not be established in case there is really a connection issue or to look nicer if I test the functionality on the emulator.