Android Studio 检查用户是否购买了安卓java订阅

lqfhib0f  于 2023-03-03  发布在  Android
关注(0)|答案(1)|浏览(115)

如何编写一个函数,在应用打开时检查用户是否已购买订阅?
这些功能仅告知用户在支付过程中成功购买订阅或取消该过程。

private PurchasesUpdatedListener purchasesUpdatedListener = new PurchasesUpdatedListener() {
    @Override
    public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
        // To be implemented in a later section.
        if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && purchases != null) {
            for (Purchase purchase : purchases) {
                handlePurchase(purchase);
                Log.d("purchaseListners", purchase.toString());
            }
            Log.d("purchaseListners", "true");

     

        } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED){
            // Handle an error caused by a user cancelling the purchase flow.
            Log.d("purchaseListners", "false");
            Constant.subscription = false;
        } else {
            Log.d("purchaseListners", "shh");
        }

    }
};

void handlePurchase(Purchase purchase) {
    ConsumeParams consumeParams =
            ConsumeParams.newBuilder()
                    .setPurchaseToken(purchase.getPurchaseToken())
                    .build();

    ConsumeResponseListener listener = new ConsumeResponseListener() {
        @Override
        public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
            if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                Constant.subscription = true;
                // Handle the success of the consume operation.

                Log.d("purchaseListners", billingResult.toString());
            }
        }
    };

    billingClient.consumeAsync(consumeParams, listener);
}
aoyhnmkz

aoyhnmkz1#

要检查用户是否已购买订阅,请使用queryPurchasesAsync()函数。

相关问题