[英]Checking if in-app purchases are supported in user's Google Play country on Android
我的 Android 应用程序中的某些功能需要应用程序内购买。 对于不支持应用内购买的国家/地区的用户,我想免费提供此功能。
如何检查用户所在的 Google Play 国家/地区是否支持应用内购买?
我尝试过的问题:
似乎无法确定 Play 商店国家/地区。
检查BILLING_UNAVAILABLE
的BillingClient.BillingResponseCode
没有帮助,因为如果“用户设备上的 Play 商店应用程序已过期”或“Google Play 无法向用户的付款方式收费”,也可能会返回此响应。
debugMessage
的BillingResult
回调返回的onBillingSetupFinished
中的BillingClient.startConnection
似乎也没有帮助,因为对于不受支持的国家/地区,它错误地表示“Google Play In-app Billing API version is less than 3”。 (我尝试使用 VPN 在中国使用。)
如果用户不在他的 Play 国家或使用 VPN,检查用户的 IP 地址将给出错误的结果。
使用位置 API 需要位置权限,如果用户在国外,也会给出错误的结果。
要检查用户的 Google Play 国家/地区是否支持应用内购买,您可以使用BillingClient
类的isBillingSupported
方法。
以下是如何使用此方法的示例:
BillingClient billingClient = BillingClient.newBuilder(context).build();
billingClient.isBillingSupported(BillingClient.SkuType.INAPP, packageName, new BillingClient.BillingResponseListener() {
@Override
public void onBillingResponse(int responseCode, @Nullable List<String> list) {
if (responseCode == BillingClient.BillingResponse.OK) {
// In-app purchases are supported in the user's Google Play country
} else {
// In-app purchases are not supported in the user's Google Play country
}
}
});
请注意,调用此方法时需要指定应用程序的packageName
。
要检查用户所在的 Google Play 国家/地区是否支持应用内购买,您可以使用BillingClient类的isBillingSupported()方法。 此方法返回一个 BillingResponse 代码,它指示isBillingSupported()请求的结果。
以下是如何在您的应用中使用此方法的示例:
BillingClient billingClient = BillingClient.newBuilder(context).setListener(new PurchasesUpdatedListener() {
@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
// Handle purchase updates here
}
}).build();
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
if (billingResponseCode == BillingClient.BillingResponse.OK) {
// Billing client is ready
int responseCode = billingClient.isBillingSupported(BillingClient.SkuType.INAPP);
if (responseCode == BillingClient.BillingResponse.OK) {
// In-app purchases are supported
} else {
// In-app purchases are not supported
}
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
}
});
请注意,在调用isBillingSupported()方法时,您需要指定要检查的应用内购买类型(例如, BillingClient.SkuType.INAPP用于一次性产品或BillingClient.SkuType.SUBS用于订阅)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.