billingClient.queryPurchases(BillingClient.SkuType.SUBS).getPurchasesList() 返回 null
billingClient.queryPurchases(BillingClient.SkuType.SUBS).getPurchasesList() returning null
我正在为 android 开发一个应用程序,提供计费服务。我已经添加了 "shop" 方法,因此用户可以下标。我的应用程序只有一个订阅待售。问题是,当用户打开应用程序时,我无法得知他是否有下标。直到现在,我已经来到这段代码,即使用户是 subscriptpurchasesResult.getPurchasesList()
returns null
:
premium = false
try{
Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.SUBS);
for (Purchase purchase : purchasesResult.getPurchasesList()) {
acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
premium = true;
}
};
handlePurchase(purchase);
}}
catch (Exception e){
e.printStackTrace();
}
处理购买方法:
void handlePurchase(Purchase purchase) {
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
if (!purchase.isAcknowledged()) {
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
billingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
}
else SplashScreenActivity.premium = true;
}
}
我很期待检查用户是否已订阅,所以我可以设置 premium = true
。直到现在,我使用的是一个丑陋的解决方案,即再次购买产品并检查它是否 returns ITEM_ALREADY_OWNED。仍在寻找更好的解决方案。
注意:我在 alpha 测试中得到了这个结果。
注意 2:This link 可能有帮助。
注意 3:This link 向其他人展示同样的问题。
你应该在这里找到解决方案!
我也使用封闭的 alpha 版本对此进行了测试,以检查订阅是否有效。当订阅不再活跃时,sku 不再在购买列表中:
如果我取消 5 分钟测试订阅,则该 sku 不再出现在列表中。所以这对我来说很好
注意 skuname 是我之前传递的标识符,例如 "mytestsub.iap1.com"
我的解决方案是:
private fun setupBillingClient() {
mBillingClient = BillingClient
.newBuilder(context!!)
.enablePendingPurchases() // Useful for physical stores
.setListener(this)
.build()
mBillingClient?.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
val purchasesResult = mBillingClient?.queryPurchases(BillingClient.SkuType.SUBS)
// Init all the purchases to false in the shared preferences (security prevention)
prefs.purchased = false
// Retrieve and loop all the purchases done by the user
// Update all the boolean related to the purchases done in the shared preferences
if (purchasesResult?.purchasesList != null) {
for (purchase in purchasesResult.purchasesList!!) {
if (purchase.isAcknowledged) {
Log.e(Config.APPTAG, purchase.sku)
when (purchase.sku) {
skuname -> {
Log.e(Config.APPTAG, " Product "+purchase.sku+" is subscribed")
// The subscription sku is found and active so then purchases to true in prefs
prefs.purchased = true
}
}
}
}
}
}
}
override fun onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
// TODO Note: It's strongly recommended that you implement your own connection retry policy and override the onBillingServiceDisconnected() method. Make sure you maintain the BillingClient connection when executing any methods.
Log.e(Config.APPTAG, "onBillingServiceDisconnected")
}
})
}
我正在为 android 开发一个应用程序,提供计费服务。我已经添加了 "shop" 方法,因此用户可以下标。我的应用程序只有一个订阅待售。问题是,当用户打开应用程序时,我无法得知他是否有下标。直到现在,我已经来到这段代码,即使用户是 subscriptpurchasesResult.getPurchasesList()
returns null
:
premium = false
try{
Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.SUBS);
for (Purchase purchase : purchasesResult.getPurchasesList()) {
acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
premium = true;
}
};
handlePurchase(purchase);
}}
catch (Exception e){
e.printStackTrace();
}
处理购买方法:
void handlePurchase(Purchase purchase) {
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
if (!purchase.isAcknowledged()) {
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
billingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
}
else SplashScreenActivity.premium = true;
}
}
我很期待检查用户是否已订阅,所以我可以设置 premium = true
。直到现在,我使用的是一个丑陋的解决方案,即再次购买产品并检查它是否 returns ITEM_ALREADY_OWNED。仍在寻找更好的解决方案。
注意:我在 alpha 测试中得到了这个结果。
注意 2:This link 可能有帮助。
注意 3:This link 向其他人展示同样的问题。
你应该在这里找到解决方案!
我也使用封闭的 alpha 版本对此进行了测试,以检查订阅是否有效。当订阅不再活跃时,sku 不再在购买列表中:
如果我取消 5 分钟测试订阅,则该 sku 不再出现在列表中。所以这对我来说很好
注意 skuname 是我之前传递的标识符,例如 "mytestsub.iap1.com"
我的解决方案是:
private fun setupBillingClient() {
mBillingClient = BillingClient
.newBuilder(context!!)
.enablePendingPurchases() // Useful for physical stores
.setListener(this)
.build()
mBillingClient?.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
val purchasesResult = mBillingClient?.queryPurchases(BillingClient.SkuType.SUBS)
// Init all the purchases to false in the shared preferences (security prevention)
prefs.purchased = false
// Retrieve and loop all the purchases done by the user
// Update all the boolean related to the purchases done in the shared preferences
if (purchasesResult?.purchasesList != null) {
for (purchase in purchasesResult.purchasesList!!) {
if (purchase.isAcknowledged) {
Log.e(Config.APPTAG, purchase.sku)
when (purchase.sku) {
skuname -> {
Log.e(Config.APPTAG, " Product "+purchase.sku+" is subscribed")
// The subscription sku is found and active so then purchases to true in prefs
prefs.purchased = true
}
}
}
}
}
}
}
override fun onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
// TODO Note: It's strongly recommended that you implement your own connection retry policy and override the onBillingServiceDisconnected() method. Make sure you maintain the BillingClient connection when executing any methods.
Log.e(Config.APPTAG, "onBillingServiceDisconnected")
}
})
}