Android应用内结算:从不调用onServiceConnected,bindService返回“false”
作者:互联网
我目前正尝试在应用中添加应用内结算功能,以便用户可以进行小额捐款.我使用最新版本的Android Studio进行开发,并遵循本指南(一步一步,我正在做的每一个完全像提到…至少我认为我做:-)):https://developer.android.com/google/play/billing/billing_integrate.html
AIDL文件放在上面提到的位置(在com.android.vending.billing包中的src / main下),我看到它是在gen文件夹下生成的.
当我测试产品的检索时,我注意到从不调用onServiceConnected方法,它在活动中实现,如下所示:
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);
}
};
对服务的绑定是这样的(在同一个活动中):
@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);
}
我注意到两件事:
>当我离开捐赠活动时,我在控制台中看到一个异常:…泄漏了ServiceConnection …最初绑定在这里 – 我看到有些人建议在应用程序上下文中替换使用bindService而不是活动,实际上如果我这样做了,问题就消失了.但是我认为这与我从未调用的onServiceConnected的主要问题无关,而且,为什么它在官方指南中调用了活动?
> bindService返回一个布尔值,我检查了返回值,它是false.所以我猜这是问题的来源.
我看到有人说必须将该服务的条目添加到AndroidManifest.xml中 – 如下所示:
试过这个,但没有区别,官方指南中也没有提到这个条目,所以我认为不需要.
我有什么想法我做错了吗?
解决方法:
实际上我在此期间发现了什么是“错误的”.
根据官方测试应用内结算指南(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返回false时会显示该消息,告知用户如果确实存在连接问题则无法建立与服务的连接,或者如果我在模拟器上测试功能则看起来更好.
标签:android,in-app-billing,android-billing,serviceconnection,onserviceconnected 来源: https://codeday.me/bug/20190717/1487667.html