播放帐单库1.0 - 没有连接

时间:2017-09-20 20:34:17

标签: android play-billing-library

我正在尝试实施谷歌新的Play Billing Library 1.0,但我无法建立连接。它适用于旧的Trivial Drive课程。

我总是得到responseCode = 3 / UNAVAILABLE

我在真实设备上运行应用程序,从Google Play下载为alpha测试程序。 有什么想法吗?

public BillingManager(Activity activity, final BillingUpdatesListener updatesListener) {
    mActivity = activity;
    mBillingUpdatesListener = updatesListener;
    mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();

    startServiceConnection(new Runnable() {
        @Override
        public void run() {
            mBillingUpdatesListener.onBillingClientSetupFinished();
            queryPurchases();
        }
    });
}

public void startServiceConnection(final Runnable executeOnSuccess) {
    mBillingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(@BillingClient.BillingResponse int responseCode) {
            if (responseCode == BillingClient.BillingResponse.OK) {
                mIsServiceConnected = true;
                if (executeOnSuccess != null) {
                    executeOnSuccess.run();
                }
            }
            mBillingClientResponseCode = responseCode;
        }

        @Override
        public void onBillingServiceDisconnected() {
            mIsServiceConnected = false;
        }
    });
}

public void initiatePurchaseFlow(final String skuId, final ArrayList<String> oldSkus, final @BillingClient.SkuType String billingType) {
    Runnable purchaseFlowRequest = new Runnable() {
        @Override
        public void run() {
            BillingFlowParams.Builder mParams = BillingFlowParams.newBuilder().setSku(skuId).setType(billingType).setOldSkus(oldSkus);
            mBillingClient.launchBillingFlow(mActivity, mParams.build());
        }
    };
    executeServiceRequest(purchaseFlowRequest);
}

private void executeServiceRequest(Runnable runnable) {
    if (mIsServiceConnected) {
        runnable.run();
    } else {
        startServiceConnection(runnable);
    }
}

@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
    if (responseCode == BillingClient.BillingResponse.OK) {
        for (Purchase purchase : purchases) {
            handlePurchases(purchase);
        }
        mBillingUpdatesListener.onPurchasesUpdated(mPurchases);
    } else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {

    } else {

    }
}

}

编辑:问题是,我将null作为ArrayList oldSkus传递。如果我没有老Skus,谁能告诉我还有什么可以通过?如果我删除.setOldSkus,它仍然有效,但我仍然想知道什么是正确的方法。

1 个答案:

答案 0 :(得分:1)

如果您使用的是模拟器,请首先检查您是否启用了Google Play服务,安装了Play商店应用,并允许您在那里购买应用。 但是在真实设备上集成Play结算要容易得多。

Play Billing库对于所有开发人员错误(包括 setOldSkus 方法的问题)都相当冗长。只需按照BillingClient中的建议启用其日志。

例如:

adb shell setprop log.tag.BillingClient VERBOSE