Android Billing 4.0.0 - 无购买结果 querySkuDetailsAsync()
Posted
技术标签:
【中文标题】Android Billing 4.0.0 - 无购买结果 querySkuDetailsAsync()【英文标题】:Android Billing 4.0.0 - No purchase result querySkuDetailsAsync() 【发布时间】:2021-08-19 07:26:49 【问题描述】:我将 android Studio 中的 Google Play Billing Library 从 3.0.3(工作正常)迁移到 4.0.0。 我检查了我的 Google Play 帐单,一切似乎都正常,并且 SKU 状态为 ACTIVE(没有危险信号)。 我已尽力遵循迁移说明@https://developer.android.com/google/play/billing/integrate#establish_a_connection_to_google_play
到目前为止,我所能做的就是与 Google Play Billing 建立良好的连接,也就是说,在 onBillingSetupFinished() 方法之后,BillingClient.BillingResponseCode.OK 响应良好,没有错误消息。
我的问题从调用 querySkuDetailsAsync() 开始:这里没有响应,甚至没有错误通知。谷歌网站非常强调这个电话,所以我觉得这就是乐趣的开始。
我已经提供了问题的示例代码。我已经使用了 Stack Overflow 的许多修复程序,但现在我真的被卡住了,真的需要它来工作。
我的问题代码如下:
'''
/*
//Using the following library in build.graddle for app module
dependencies
def billing_version = "4.0.0"
implementation "com.android.billingclient:billing:$billing_version"
*/
StringBuilder builder4SKUInfo;
private void get_Subscribe2_Characters()
Subscribe2_Characters_Button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
//I Toggle Visibility of Views Here
billingClient.startConnection(new BillingClientStateListener()
//Android Studio auto-prompts to generate onBillingSetupFinished & onBillingServiceDisconnected
@Override
public void onBillingSetupFinished(@NonNull BillingResult billingResultC)
if (billingResultC.getResponseCode() == BillingClient.BillingResponseCode.OK)
//BillingResponseCode is OK here: Works Just Fine!
//The problem starts below
String skuToSell = "MySKU_Character_001"; //In my project, the SKU is cut-pasted from Google Play Console
List<String> skuList = new ArrayList<> ();
skuList.add(skuToSell);
SkuDetailsParams.Builder params = SkuDetailsParams
.newBuilder()
.setSkusList(sku_Details) //
.setType(BillingClient.SkuType.SUBS);
billingClient.querySkuDetailsAsync(params.build(),
new SkuDetailsResponseListener()
@Override
public void onSkuDetailsResponse(@NonNull BillingResult billingResult, @NonNull List<SkuDetails> PurchaseDetailsList)
//NOTHING! Not getting BillingResult
//Problem seems to at this point
if (PurchaseDetailsList.size() > 0)
//NOTHING! Not getting size
for (SkuDetails PurchaseSKU_Info : PurchaseDetailsList)
builder4SKUInfo = new StringBuilder(300);
if (PurchaseSKU_Info.getSku().contains("MySKU_Character_001"))
String getSKUInfo = (
"\nTitle [Query]: " + PurchaseSKU_Info.getTitle()
+ "\n\nDetails: " + PurchaseSKU_Info.getDescription()
+ "\n\nDuration: " + PurchaseSKU_Info.getSubscriptionPeriod()
+ "\n\nPrice" + PurchaseSKU_Info.getPrice()
+ "\n\nAvoid Problems:\nUpdated Subscription Settings on Google Play"
+ "\n\nIMPORTANT: NOT Transferable"
+ "\n\n For this device only\n");
//+ "\nOther SKUs: " + SKU_Info.getSku()
//"001 = " + billingResultB.getResponseCode()
//+ "\nList Size: " + PurchaseDetailsList.size());
builder4SKUInfo.append(getSKUInfo); //The result I need to use elsewhere
else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED)
//No Google Play response for this
else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.ITEM_NOT_OWNED)
//No Google Play response for this
else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED)
//Do something about cancels
else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.BILLING_UNAVAILABLE)
//No Google Play response for this
else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.SERVICE_DISCONNECTED)
//No Google Play response for this
else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.SERVICE_TIMEOUT)
//No Google Play response for this
else
//Following Toast does not show
String SomethingWrong = "Somethings is Wrong" +
"\nUpdate Your Google Play Billing Info" +
"\nCheck Internet Connection";
Toast.makeText(KH.this, SomethingWrong, Toast.LENGTH_LONG).show();
);
@Override
public void onBillingServiceDisconnected()
//Following Toast does not show
String BillingServiceDisconnected = "Billing Service Disconnected" +
"\nUpdate Your Google Play Billing Info" +
"\nCheck Internet Connection";
Toast.makeText(KH.this, BillingServiceDisconnected, Toast.LENGTH_LONG).show();
);
);
'''
【问题讨论】:
sku_Details
它没有在任何地方声明...... ?????
@ekashking 。 . . “sku_Details”在 Google 的 GitHub 示例中被称为 MutableLiveData HashMap。然后,您可以传输地图和“做东西”,例如添加 SKU、检测购买状态等。地图与 Main 位于不同的类中。这是GitHub link to the Map architecture
【参考方案1】:
所以我勇敢地在问题跟踪页面上询问了 Google 的人员,他们适当而迅速地回答:“我们现在将结果发布到后台线程而不是 UIThread ......”
我马上就知道我的方法不对。如果结果被传递到后台线程,我不得不放弃 3.x 计费方法并从头开始。
我再次联系 Google 以获取示例,他们向我发送了他们的 GitHub @https://github.com/android/play-billing-samples/tree/main/TrivialDriveJava
该示例类似于“意图”,但代码声明比函数选择多得多:有几个类、方法和文件需要处理。因此,要修复计费 4.x,最简单的方法是将示例复制到我的应用程序中,减少错误,将我不需要的方法变灰,最后覆盖我的视图,重构类(再次修复错误)并创建新的用户工作流程。
【讨论】:
感谢您的回答。在我这边,我在 onSkuDetailsResponse 回调中打开了一个自定义对话框 (myDialog.show)。它适用于 3.0.3,但不适用于 4.0.0。所以我想这是因为我的对话框现在“显示”在后台线程中,而不是之前的 UI 中。我会留在 3.0.3【参考方案2】:按照@Maasaivatar 的回答,它在主线程上运行 SkuDetailsResponseListener 后工作:
billingClient.querySkuDetailsAsync(params.build(), (billingResult, list) ->
runOnUiThread(() ->
// same code as before
));
【讨论】:
这行得通。但是使用这种方法有什么问题吗?有人请解释一下吗? 谢谢!我也为 queryPurchasesAsync 的 onQueryPurchasesResponse 侦听器做到了这一点。以上是关于Android Billing 4.0.0 - 无购买结果 querySkuDetailsAsync()的主要内容,如果未能解决你的问题,请参考以下文章
google应用内购(in app billing),点击购买无响应
为啥 android-inapp-billing-v3 库需要两次尝试购买?
测试 Android In App Billing 真实购买
android billing inApp/subscription 找不到明确的活动 ProxyBillingActivity