gpt4 book ai didi

android - 使用 Play 结算库检查订阅状态

转载 作者:行者123 更新时间:2023-12-04 13:45:16 47 4
gpt4 key购买 nike

在我的应用程序中,我有订阅。我使用 IInAppBillingService 使用经典方式实现了购买流程并检查订阅状态。我使用这种方式遇到了一些问题。大约 1% 的用户提示在成功购买订阅后,这些订阅在应用程序中不起作用。对于这个问题,我向 Google Pla 寻求支持。他们建议使用新的 Play Billing Library。我查看了说明 https://codelabs.developers.google.com/codelabs/play-billing-codelab/#6 中的示例.但是,此示例不考虑检查订阅状态。

我也在页面 https://developer.android.com/google/play/billing/billing_library.html 上找到了以下:“Google Play 服务返回用户帐户登录到设备的购买。如果请求成功,Play 计费库将查询结果存储在购买对象列表中。要检索列表,请调用 getPurchasesList PurchasesResult 对象上的 () 方法。”

因此,对于教程中的 BillingManager 类,我添加了一个代码来检查订阅的购买情况:

public Purchase.PurchasesResult querySubscriptionPurchases() {
return mBillingClient.queryPurchases(BillingClient.SkuType.SUBS);
}

在 MainActivity 中使用 BillingManager:
mBillingManager = new BillingManager(this);
Purchase.PurchasesResult purchasesResult = mBillingManager.querySubscriptionPurchases();
if (purchasesResult != null) {
List<Purchase> p = purchasesResult.getPurchasesList();
}

当我运行应用程序时,在 logcat 中我看到“I/BillingManager: onBillingSetupFinished() response: 0”。那么连接成功了吗?

但是购买结果为空,我无法获取购买列表来探索此列表包含的内容以获取 Activity 订阅的 SKU。在使用 IInAppBillingService 的 Debug模式经典方式中成功返 repo 买,我有一个有效的测试订阅并且它有效。

BillingManager 的完整列表:
public class BillingManager implements PurchasesUpdatedListener {
private static final String TAG = "BillingManager";

private final BillingClient mBillingClient;
private final Activity mActivity;

private static final HashMap<String, List<String>> SKUS;
static
{
SKUS = new HashMap<>();
SKUS.put(BillingClient.SkuType.SUBS, Arrays.asList("no_ads", "sub2", "sub3"));
}

public BillingManager(Activity activity) {
mActivity = activity;
mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponse) {
if (billingResponse == BillingClient.BillingResponse.OK) {
Log.i(TAG, "onBillingSetupFinished() response: " + billingResponse);
} else {
Log.w(TAG, "onBillingSetupFinished() error code: " + billingResponse);
}
}
@Override
public void onBillingServiceDisconnected() {
Log.w(TAG, "onBillingServiceDisconnected()");
}
});
}

@Override
public void onPurchasesUpdated(@BillingClient.BillingResponse int responseCode,
List<Purchase> purchases) {
Log.d(TAG, "onPurchasesUpdated() response: " + responseCode);
}

public List<String> getSkus(@BillingClient.SkuType String type) {
return SKUS.get(type);
}

private void startServiceConnectionIfNeeded(final Runnable executeOnSuccess) {
if (mBillingClient.isReady()) {
if (executeOnSuccess != null) {
executeOnSuccess.run();
}
} else {
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponse) {
if (billingResponse == BillingClient.BillingResponse.OK) {
Log.i(TAG, "onBillingSetupFinished() response: " + billingResponse);
if (executeOnSuccess != null) {
executeOnSuccess.run();
}
} else {
Log.w(TAG, "onBillingSetupFinished() error code: " + billingResponse);
}
}

@Override
public void onBillingServiceDisconnected() {
Log.w(TAG, "onBillingServiceDisconnected()");
}
});
}
}

public Purchase.PurchasesResult querySubscriptionPurchases() {
return mBillingClient.queryPurchases(BillingClient.SkuType.SUBS);
}

public void querySkuDetailsAsync(@BillingClient.SkuType final String itemType,
final List<String> skuList, final SkuDetailsResponseListener listener) {
// Specify a runnable to start when connection to Billing client is established
Runnable executeOnConnectedService = new Runnable() {
@Override
public void run() {
SkuDetailsParams skuDetailsParams = SkuDetailsParams.newBuilder()
.setSkusList(skuList).setType(itemType).build();
mBillingClient.querySkuDetailsAsync(skuDetailsParams,
new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(int responseCode,
List<SkuDetails> skuDetailsList) {
listener.onSkuDetailsResponse(responseCode, skuDetailsList);
}
});
}
};

// If Billing client was disconnected, we retry 1 time and if success, execute the query
startServiceConnectionIfNeeded(executeOnConnectedService);
}

public void startPurchaseFlow(final String skuId, final String billingType) {
// Specify a runnable to start when connection to Billing client is established
Runnable executeOnConnectedService = new Runnable() {
@Override
public void run() {
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setType(billingType)
.setSku(skuId)
.build();
mBillingClient.launchBillingFlow(mActivity, billingFlowParams);
}
};

// If Billing client was disconnected, we retry 1 time and if success, execute the query
startServiceConnectionIfNeeded(executeOnConnectedService);
}

public void destroy() {
mBillingClient.endConnection();
}
}

最佳答案

我使用了 TrivialDrive 示例中的代码。现在它起作用了。

在 MainActivity 中:

mUpdateListener = new myBillingUpdateListener();
mBillingManager = new BillingManager(this, getUpdateListener());


@Override
public void onPurchasesUpdated(List<Purchase> purchaseList) {

for (Purchase purchase : purchaseList) {
switch (purchase.getSku()) {
case "no_ad":
doNotShowAds = true;
break;
}
}
}

在计费管理器中:
@Override
public void onPurchasesUpdated(int resultCode, List<Purchase> purchases) {
if (resultCode == BillingClient.BillingResponse.OK) {
for (Purchase purchase : purchases) {
handlePurchase(purchase);
}
mBillingUpdatesListener.onPurchasesUpdated(mPurchases);
} else if (resultCode == BillingClient.BillingResponse.USER_CANCELED) {
Log.i(TAG, "onPurchasesUpdated() - user cancelled the purchase flow - skipping");
} else {
Log.w(TAG, "onPurchasesUpdated() got unknown resultCode: " + resultCode);
}
}

关于android - 使用 Play 结算库检查订阅状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49898924/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com