gpt4 book ai didi

android - 应用启动时如何查询用户购买 - Play Billing 1.0

转载 作者:搜寻专家 更新时间:2023-11-01 08:24:37 25 4
gpt4 key购买 nike

这是我的计费经理

public class BillingManager implements PurchasesUpdatedListener {
private BillingClient mBillingClient;
private Activity mActivity;

public BillingManager(Activity activity) {
mActivity = activity;
mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();
startServiceConnectionIfNeeded(null);
}

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()");
}
});
}
}

下面的 Listener 只有在完成 google play 库时才会执行

@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
Log.d(TAG, "onPurchasesUpdated: ResponseCode = "+ responseCode);
Log.d(TAG, "onPurchasesUpdated: Purchase = "+purchases);
if (responseCode == BillingClient.BillingResponse.OK && purchases != null) {
//if user purchased something
} else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
// Handle an error caused by a user cancelling the purchase flow.
} else {
// Handle any other error codes.
}
}

最佳答案

除了成功的 onBillingSetupFinished 回调之外,就在您的 Activity 的 onCreate 或更好的 onResume 中(这将防止在应用程序之间切换时丢失购买的潜在问题)。

所有类似问题请查看最新TrivialDrive_v2 .

例如queryPurchase 被触发 herehere那里。

关于android - 应用启动时如何查询用户购买 - Play Billing 1.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46470190/

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