我正在我的 Android 应用程序中实施 Google 计费订阅。 我已经关注了Google Play Billing官方文档。 我已在 Play 管理中心创建订阅并向我的应用程序添加相关功能。
问题是,没有来自 Play 控制台的应用订阅,总是
billingClient!!.queryProductDetailsAsync
方法返回一个空的 productDetailsList
.
谁能帮我找出这里的问题?
这是我的实现,
在我的应用级gradle文件中,
implementation "com.android.billingclient:billing-ktx:5.1.0"
在我的订阅文件中
private var billingClient: BillingClient? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//Setup billing
billingSetup()
}
private fun billingSetup() {
billingClient = BillingClient.newBuilder(this)
.setListener(purchasesUpdatedListener)
.enablePendingPurchases()
.build()
//Connect to Google Play
connectToGooglePlay()
}
private fun connectToGooglePlay() {
billingClient!!.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
Log.i("payment_flow", "OnBillingSetupFinish connected")
queryProduct()
} else {
Log.i("payment_flow", "OnBillingSetupFinish failed")
}
}
override fun onBillingServiceDisconnected() {
Log.i("payment_flow", "OnBillingSetupFinish connection lost")
//Re-connect to Google Play
connectToGooglePlay()
}
})
}
private fun queryProduct() {
val productList = ImmutableList.of(
Product.newBuilder()
.setProductId("monthly_plan")
.setProductType(BillingClient.ProductType.SUBS)
.build(),
Product.newBuilder()
.setProductId("yearly_plan")
.setProductType(BillingClient.ProductType.SUBS)
.build()
)
val queryProductDetailsParams = QueryProductDetailsParams.newBuilder()
.setProductList(productList).build()
billingClient!!.queryProductDetailsAsync(queryProductDetailsParams) {
billingResult: BillingResult, productDetailsList: List<ProductDetails> ->
Log.d("payment_flow", "onProductDetailsResponse: 1: $billingResult")
if (productDetailsList.isNotEmpty()) {
tmpProductDetailsList.addAll(productDetailsList)
Log.d("payment_flow", "onProductDetailsResponse: " + productDetailsList.size)
} else {
Log.i("payment_flow", "onProductDetailsResponse: No products")
}
}
}
提前致谢
好吧,我的问题是我的产品没有显示在我的 UI 中,所以我在一个线程中循环和显示我的产品。下面是我的代码,希望对你有帮助。除此之外,你的代码看起来和我的一样。所以关键点,
runOnUiThread
。我的产品现在正在完美加载,可以购买了。
billingClient.queryProductDetailsAsync(
params,
(billingResult, productDetailsList) -> {
// Process the result
Log.d(TAG,"Got list queryProductDetailsAsync "+productDetailsList.size());
for (ProductDetails productDetails : productDetailsList){
Log.d(TAG,"Got productDetails: "+productDetails.getName()+"\nDesc: "+productDetails.getDescription());
String productName = productDetails.getName();
String productPrice = productDetails.getSubscriptionOfferDetails().get(0)
.getPricingPhases().getPricingPhaseList().get(0).getFormattedPrice();
String productID = productDetails.getProductId().toString();
runOnUiThread(new Runnable() {
@Override
public void run() {
if (productDetails.getProductId().equals(Subscriptions.MONTHLY)) {
Log.d(TAG,"Got product Subscriptions.MONTHLY");
cvMonth.setVisibility(View.VISIBLE);
tvTitleMonth.setText(productName+"\n"+productPrice);
tvDescMonth.setText(productDetails.getDescription());
btnBuyMonth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
launchPurchaseFlow(productDetails);
}
});
}
if (productDetails.getProductId().equals(Subscriptions.BI_ANNUAL)) {
Log.d(TAG,"Got product Subscriptions.BI_ANNUAL");
cvSixMonth.setVisibility(View.VISIBLE);
tvTitleSixMonth.setText(productName+"\n"+productPrice);
tvDescSixMonth.setText(productDetails.getDescription()
+"\nYou save 40% compared to Monthly Subscription");
btnBuySixMonth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
launchPurchaseFlow(productDetails);
}
});
}
if (productDetails.getProductId().equals(Subscriptions.ANNUAL)) {
Log.d(TAG,productName+"\n"+productPrice);
cvAnnual.setVisibility(View.VISIBLE);
tvTitleAnnual.setText(productName+"\n"+productPrice);
tvDescAnnual.setText(productDetails.getDescription()
+"\nYou save 40% compared to Monthly Subscription. Only " +
"pay once a year.");
btnBuyAnnual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
launchPurchaseFlow(productDetails);
}
});
}
if(loadingDialog != null){
loadingDialog.dismiss();
}
}
});
}
}
);
对我来说,问题是我刚刚在谷歌游戏控制台中创建了订阅。有两种基本计划,一种是每周一次,另一种是每月一次。它返回的是每周的详细信息,而不是每月的。在代码中搜索了这么多之后,我认为新创建的订阅可能需要时间,结果谷歌将产品缓存在您的设备中。我在我的设备上选择了 Google Play 应用程序,在应用程序信息页面中我刚刚选择了“清除数据”所以所有缓存数据都被清除了,现在它工作正常并返回所有产品。