您可以使用以下代码示例来解决该问题:
private boolean isUserInSupportedCountry(Context context) {
String countryCode = context.getResources().getConfiguration().locale.getCountry();
return countryCode.equals("US") || countryCode.equals("FR") || countryCode.equals("GB");
}
private boolean isProductAvailableForPurchase(Context context, String productId) {
BillingClient billingClient = BillingClient.newBuilder(context).build();
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
List skuList = new ArrayList<>();
skuList.add(productId);
SkuDetailsParams params = SkuDetailsParams.newBuilder()
.setSkusList(skuList)
.setType(BillingClient.SkuType.INAPP)
.build();
billingClient.querySkuDetailsAsync(params, new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(@NonNull BillingResult billingResult, @Nullable List skuDetailsList) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && skuDetailsList != null) {
for (SkuDetails skuDetails : skuDetailsList) {
if (skuDetails.getSku().equals(productId)) {
return true;
}
}
}
}
});
}
}
@Override
public void onBillingServiceDisconnected() {
}
});
return false;
}
private void handlePurchaseError(Context context, BillingResult result) {
if (result.getResponseCode() == BillingClient.BillingResponseCode.ITEM_UNAVAILABLE) {
if