【问题标题】:How do i query for user purchases When App Starts - Play Billing 1.0应用启动时如何查询用户购买 - Play Billing 1.0
【发布时间】:2017-09-28 13:18:00
【问题描述】:

这是我的帐单经理

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

只有在完成谷歌播放库后才会执行下面的监听器

@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.
        }
    }

【问题讨论】:

    标签: android google-play in-app-purchase in-app-billing play-billing-library


    【解决方案1】:

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

    对于所有类似的问题,请查看最新的TrivialDrive_v2

    例如queryPurchase 在herehere 那里触发。

    【讨论】:

    • 谢谢,我现在明白了。 BillingClient.queryPurchases() 成功了
    • 那么,如果您接受我的回答来帮助有类似问题的人,那就太好了。
    猜你喜欢
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 2019-10-20
    相关资源
    最近更新 更多