gpt4 book ai didi

android - 播放计费库连接和查询sku详情

转载 作者:太空狗 更新时间:2023-10-29 13:05:43 25 4
gpt4 key购买 nike

我需要一些关于如何建立连接和查询 sku 详细信息的信息。我正在编写教程并将应用内计费逻辑复制到我的应用中。

https://codelabs.developers.google.com/codelabs/play-billing-codelab

我按照教程没有任何问题。问题在于如何建立连接,然后查询 sku 详细信息。

当我创建 BillingManager 类的实例时,它将尝试建立连接 -

public BillingManager(Activity activity) {
mActivity = activity;
mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponse) {
if (billingResponse == BillingClient.BillingResponse.OK) {
Log.i(TAG, "onBillingSetupFinished() response: " + billingResponse);
} else {
Log.w(TAG, "onBillingSetupFinished() error code: " + billingResponse);
}
}
@Override
public void onBillingServiceDisconnected() {
Log.w(TAG, "onBillingServiceDisconnected()");
}
});
}

然后,我将进行异步查询以获取 sku 详细信息 -

private void handleManagerAndUiReady() {
// Start querying for SKUs
List<String> inAppSkus = mBillingProvider.getBillingManager()
.getSkus(SkuType.INAPP);
mBillingProvider.getBillingManager().querySkuDetailsAsync(SkuType.INAPP,
inAppSkus,
new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(int responseCode,
List<SkuDetails> skuDetailsList) {
if (responseCode == BillingResponse.OK
&& skuDetailsList != null) {
for (SkuDetails details : skuDetailsList) {
Log.w(TAG, "Got a SKU: " + details);
}
}
}
});

// Show the UI
displayAnErrorIfNeeded();
}

然后我让监听器收到错误,但连接没有任何问题。

D/StoreListFragment: onCreate
I/StoreListFragment: SkuDetailsResponseListener response code: -1
D/StoreListFragment: onViewCreated
I/BillingManager: onBillingSetupFinished() response: 0

所以我不得不琢磨了一段时间,放弃了去查Play Billing Library的基础——

https://medium.com/exploring-android/exploring-the-play-billing-library-for-android-55321f282929

这就是我找到解决方案的地方,我只是将查询放在成功连接的连接中。我意识到 play billing 库在查询 sku 详细信息之前不会检查连接,还是我在某处做错了,因为教程工作正常?

private void createBillingClient() {
mBillingClient = BillingClient.newBuilder(getActivity()).setListener(this).build();

mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(int billingResponse) {
if (billingResponse == BillingClient.BillingResponse.OK) {
Log.i(TAG, "onBillingSetupFinished() response: " + billingResponse);

//setting up a listener for the queries
SkuDetailsResponseListener responseListener = new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(int responseCode,
List<SkuDetails> skuDetailsList) {
Log.i(TAG, "response code: " + responseCode);
}
};

List<String> skuList = Arrays.asList("sku_01", "sku_02");
SkuDetailsParams skuDetailsParams = SkuDetailsParams.newBuilder()
.setSkusList(skuList).setType(BillingClient.SkuType.SUBS).build();

mBillingClient.querySkuDetailsAsync(skuDetailsParams, responseListener);
} else {
Log.w(TAG, "onBillingSetupFinished() error code: " + billingResponse);
}
}

@Override
public void onBillingServiceDisconnected() {
Log.w(TAG, "onBillingServiceDisconnected()");
}
});
}

我尝试了这个逻辑来检查连接是否准备好,然后像普通驱动器一样执行可运行的 - 引用这个 url。看起来逻辑不检查计费连接是否挂起....

https://github.com/zumrywahid/in_app_example

Is Billing Client connected? : false
Client is already in the process of connecting to billing service.
onBillingSetupFinished() error code: 5

最佳答案

BillingManager 构造函数已经开始连接,如果您初始化管理器并立即调用任何将 runnable 传递给 executeServiceRequest(Runnable runnable) 的方法,也会尝试同时开始连接。您可以在构造函数中禁用 startServiceConnection(),因为连接状态始终在 executeServiceRequest() 中检查并在需要时启动连接。

关于android - 播放计费库连接和查询sku详情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48304031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com