Android 应用内结算:永远不会调用 onServiceConnected 和 bindService returns "false"

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

我目前正在尝试将应用内结算添加到我的应用中,以便用户可以进行小额捐赠。我使用最新版本的 Android Studio 进行开发,并按照本指南进行操作(一步一步,我正在做的每件事都完全按照提到的...至少我认为我是这样做的 :-)):https://developer.android.com/google/play/billing/billing_integrate.html

AIDL 文件位于上述位置(在包 com.android.vending.billing 中的 src/main 下),我看到它正在 gen 文件夹下生成。

当我测试产品检索时,我注意到方法 onServiceConnected 从未被调用,它在 activity 中实现如下:

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);
   }
};

服务的绑定是这样的(在同一个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);
}

我注意到两件事:

我看到有人说必须将服务条目添加到 AndroidManifest.xml - 如下所示:

试过这个,但没有什么区别,而且官方指南中也没有提到这个条目,所以我认为不需要它。

知道我做错了什么吗?

我像谷歌这个例子一样实现了应用内购买,如果你阅读它,你会在 class IabHelper 中看到上面的代码(它为你实现)。

https://github.com/googlesamples/android-play-billing/tree/master/TrivialDrive

实际上我在这期间发现了"wrong"是什么。

根据官方测试应用内结算指南(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.

不幸的是,此信息不在其他指南中,我通常先在模拟器上测试所有内容。将应用程序部署到真实设备并在那里进行测试后,很明显没有问题。

我现在添加了一条消息,该消息会在 bindService returns false 时出现,通知用户无法建立与服务的连接,以防万一如果我在模拟器上测试功能,确实存在连接问题或看起来更好。