【发布时间】:2017-03-04 03:48:39
【问题描述】:
我想在我的应用中包含应用内购买,但我无法将我的活动绑定到应用内结算服务。
我已经完成了页面https://developer.android.com/training/in-app-billing/preparing-iab-app.html中提到的所有步骤
在物理设备中调试我发现问题出在 IabHelper 类的下一个命令中:
mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
我注意到它不起作用,因为程序没有停止在ServiceConnection实例的两个方法中的任何一个,也就是说它没有停止在onServiceConnected()和onServiceDisconnected()
我直接在我的 Activity 中使用相同的命令进行了测试,并且与应用内计费服务的绑定成功。
因此,如果从 Activity 请求绑定,则绑定正在工作,但从 IabHelper 类请求时绑定不工作。
我的问题是,如何将我的活动绑定到 IabHelper 类的计费服务?
下面是从 IabHelper 调用 startSetup 方法的代码:
mHelper = new IabHelper(this, publicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener()
{
@Override
public void onIabSetupFinished(IabResult result)
{
if(!result.isSuccess())
{
mHelper=null;
return;
}
if (mHelper == null) return;
}
});
这是 IabHelper 类中绑定到服务的代码:
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
List<ResolveInfo> intentServices = mContext.getPackageManager().queryIntentServices(serviceIntent, 0);
if (intentServices != null && !intentServices.isEmpty())
{
// service available to handle that Intent
mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
}
这是我用来直接从活动绑定到服务的代码:
private 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);
}
};
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
this.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
提前感谢您的帮助
【问题讨论】:
-
IabHelper 类中的
mContext是什么?是“instanceof”Activity吗?