Play Billing Library 1.0 - 无连接

Play Billing Library 1.0 - no connection

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

我总是收到 responseCode = 3/UNAVAILABLE

我 运行 真实设备上的应用程序,作为 alpha 测试人员从 google play 下载。 有什么想法吗?

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,它会起作用,但我仍然想知道什么是正确的方法。

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

并且 Play Billing 库对于所有开发人员错误(包括您使用 setOldSkus 方法的问题)都相当冗长。只需按照 BillingClient.

中的建议启用其日志即可

例如:

adb shell setprop log.tag.BillingClient VERBOSE