【问题标题】:Not binding in-app billing service from IabHelper不绑定来自 IabHelper 的应用内计费服务
【发布时间】: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吗?

标签: android in-app-billing


【解决方案1】:

我才意识到我的错误,我在 startSetup 完成之前调用了 queryInventoryAsync 方法。

这是我的错误:

mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener()
{
    @Override
    public void onIabSetupFinished(IabResult result)
    {
        if(!result.isSuccess())
        {
            mHelper=null;
            return;
        }
    }
});

try {mHlpr.queryInventoryAsync(true, itemList, mQueryListener);}
catch (IabHelper.IabAsyncInProgressException e) {e.printStackTrace();}

为了更正,我将代码更改为:

mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener()
{
    @Override
    public void onIabSetupFinished(IabResult result)
    {
        if(!result.isSuccess())
        {
            mHelper=null;
            return;
        }
        else if(result.isSuccess())
        {
            try {mHlpr.queryInventoryAsync(true, itemList, mQueryListener);}
            catch (IabHelper.IabAsyncInProgressException e) {e.printStackTrace();}
        }
    }
});

当我直接从活动中测试绑定时,我已经删除了查询命令,这就是它在活动中起作用的原因;当我从 IabHelper 类进行测试时,我正在调用查询命令

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 2016-02-18
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多