我需要你在连接和查询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计费库的基础知识 -
https://medium.com/exploring-android/exploring-the-play-billing-library-for-android-55321f282929
那就是我找到解决方案的地方,我只是将查询放在成功连接的连接中。我意识到play计费库在查询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