Xamarin In App结算组件未连接到Google Play

时间:2017-01-30 19:45:46

标签: xamarin.android google-play-services in-app-billing

我已经在它的Component页面上浏览了Xamarin IAB'教程'。我将组件和Google Play Billing Lib安装到我的应用程序中,将我的apk发布到Alpha中的Google Play开发者控制台,并将Dev Console上的产品添加到应用程序中。但是,当我尝试在手机上测试应用程序时,无论何时我点击任何购买按钮都没有任何反应。按钮本身工作正常我通过推送其他通知,改变颜色等来测试它们。它们与其他所有东西一起工作,但是当涉及购买时没有任何事情发生,没有弹出窗口,没有缓冲或尝试连接,几乎没有。我认为我的应用程序从未连接到Google Play,我不知道为什么。

我的主要活动

private InAppBillingServiceConnection _serviceConnection;
    private string publicKey = "my public key";

    private IList<Product> _products;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Establish Connection to Google Play Store
        _serviceConnection = new InAppBillingServiceConnection(this, publicKey);

        _serviceConnection.OnConnected += async () =>
        {
            // Load available products and any purchases
            await RequestProducts();
        };

        // Attempt to connect to the service
        _serviceConnection.Connect();

        IAPHelper.Instance.Initalize(_products, _serviceConnection);

        var g = new Game1();
        SetContentView(g.Services.GetService<View>());
        g.Run();
    }

    // Request a list of available products that the user can purchase by providing alist of 
    protected async Task RequestProducts()
    {
        _products = await _serviceConnection.BillingHandler.QueryInventoryAsync(new List<string>{
            ReservedTestProductIDs.Purchased,
            ReservedTestProductIDs.Canceled,
            ReservedTestProductIDs.Refunded,
            ReservedTestProductIDs.Unavailable
        }, ItemType.Product);

        // Were any products returned?
        if (_products == null)
        {
            // No, abort
            return;
        }
    }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);

我的助手方法

private IList<Product> _products;
    private InAppBillingServiceConnection _serviceConnection;

    public void Initalize(IList<Product> _products, InAppBillingServiceConnection _serviceConnection)
    {
        this._products = _products;
        this._serviceConnection = _serviceConnection;
    }

    // Called when a product is clicked to buy
    public bool ProductPurchasing(string id)
    {
        Product _selectedProduct = null;
        try
        {
            for (int i = 0; i < _products.Count; i++)
            {
                if (id == _products[i].ProductId)
                {
                    _selectedProduct = _products[i];
                    break;
                }
            }

            _serviceConnection.BillingHandler.BuyProduct(_selectedProduct);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

0 个答案:

没有答案