1

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

4

1 回答 1

1

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

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

例如:

adb shell setprop log.tag.BillingClient VERBOSE
于 2017-09-28T22:10:01.213 回答