我想將應用內購買包括到我的應用中,但我無法將我的活動綁定到應用內結算服務。未綁定IabHelper的應用內結算服務
我已經做在頁面https://developer.android.com/training/in-app-billing/preparing-iab-app.html
調試的物理設備中,我發現,這個問題是在IabHelper類的下一個命令中提到的所有步驟:
mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
我注意到,它不起作用,因爲程序不停止在ServiceConnection實例的兩個方法中的任何一個,也就是說,它不停止在onServiceConnected()和onServiceDisconnected()上。 est直接在我的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嗎? – AADProgramming