android - Play Billing Library 1.0 - 无连接

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

最佳答案

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

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

例如:

adb shell setprop log.tag.BillingClient VERBOSE

关于android - Play Billing Library 1.0 - 无连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46331322/

相关文章:

android - Bootstrap 3 响应不适用于 android internet explorer 的移动设备

android - 如何使用Mobile SDK获取DJI Phantom 4 Pro的避障传感器数据

android - Google Play 结算库 : Determine base plan/expiry of subscription purchase

google-api - 即使在服务器到服务器身份验证后也无法使用 Google Play Developer API

java - queryPurchases() vs queryPurchaseHistoryAsync() 为了 'restore' 功能?

android - flutter - 如何为文本的一部分着色(即在句子中间)

android - 如何从 ListView 中获取不在 View 中的数据?

Android:如何垂直和水平对齐 View

play-billing-library - 使用 com.android.billingclient.api 通过开发人员负载进行订阅购买

in-app-billing - 如何区分可消费产品和非消费产品?