Pat*_*ric 3 android serviceconnection in-app-billing android-billing onserviceconnected
我目前正尝试在应用中添加应用内结算功能,以便用户可以进行小额捐款.我使用最新版本的Android Studio进行开发,并遵循本指南(一步一步,我正在做的每一个完全像提到...至少我认为我做:-)):https://developer.android.com /google/play/billing/billing_integrate.html
AIDL文件放在上面提到的位置(在src/maincom.android.vending.billing包中),我看到它是在该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);
}
};
Run Code Online (Sandbox Code Playgroud)
对服务的绑定是这样的(在同一个活动中):
@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);
}
Run Code Online (Sandbox Code Playgroud)
我注意到两件事:
... has leaked ServiceConnection ... that was originally bound here- 我看到有些人建议在应用程序上下文中替换使用bindService而不是活动,事实上如果我这样做,问题就消失了.但我认为这与我onServiceConnected从未打过电话的主要问题无关,而且为什么在官方指南中呼吁这项活动呢?bindService返回一个布尔值,我检查了返回值,它是假的.所以我猜这是问题的来源.我看到有人说必须将该服务的条目添加到AndroidManifest.xml中 - 如下所示:
试过这个,但没有区别,官方指南中也没有提到这个条目,所以我认为不需要.
我有什么想法我做错了吗?
Pat*_*ric 10
实际上我在此期间发现了什么是"错误的".
根据官方测试应用内结算指南(https://developer.android.com/google/play/billing/billing_testing.html):
在Android设备上安装您的应用程序.
您无法使用模拟器测试应用内结算 ; 您必须在设备上安装应用程序才能测试应用内结算.
不幸的是,这些信息不在其他指南中,我通常首先在模拟器上测试所有内容.将应用程序部署到真实设备并在那里进行测试后,很明显没有问题.
我现在包含一条消息,该消息在bindService返回false时出现,通知用户在出现连接问题时无法建立与服务的连接,或者如果我在模拟器上测试功能则看起来更好.
| 归档时间: |
|
| 查看次数: |
907 次 |
| 最近记录: |