如何在Android的Google Play结算中获取有关应用内购买的信息

时间:2019-06-27 09:17:15

标签: android android-billing

我想获取有关如何获取android studio中具有或不具有互联网连接的已购买产品的详细信息。

据我所知,在Google Play计费库中有2种方法来检索信息。

1. PurchasesResult purchasesResult = billingClient.queryPurchases(SkuType.INAPP);

2. billingClient.queryPurchaseHistoryAsync(SkuType.INAPP,
                                         new PurchaseHistoryResponseListener() {
    @Override
    public void onPurchaseHistoryResponse(BillingResult billingResult,
                                          List<Purchase> purchasesList) {
        if (billingResult.getResponseCode() == BillingResponse.OK
                && purchasesList != null) {
            for (Purchase purchase : purchasesList) {
                // Process the result.
            }
         }
    }
});

但是我不知道如何检索信息。

1 个答案:

答案 0 :(得分:0)

This is the way I have implemented In-App Purchase. It is working smoothly. Hope it helps.

mBillingClient = BillingClient.newBuilder(this).setListener(new PurchasesUpdatedListener() {
            @Override
            public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {

                //  Log.d("anupam", responseCode + "");
                if (responseCode == BillingClient.BillingResponse.OK && purchases != null) {
                    for (Purchase purchase : purchases) {
                    //List of purchases
                    }
                } else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
                    Toast.makeText(SplashActivity.this, "Sorry, you have canceled purchase Subscription.", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(SplashActivity.this, "Sorry, some error occurred.", Toast.LENGTH_SHORT).show();
                }
            }
        }).build();

        mBillingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {

                if (billingResponseCode == BillingClient.BillingResponse.OK) {
                    // The billing client is ready. You can query purchases here.

                    final Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.SUBS);
                    for (Purchase sourcePurchase : purchasesResult.getPurchasesList()) {
                        if (sourcePurchase != null) {

                            ConsumeResponseListener listener = new ConsumeResponseListener() {
                                @Override
                                public void onConsumeResponse(final int responseCode, final String purchaseToken) {
                                    // Log.d("anupam2", responseCode + "  <------->  "+ purchasesResult.getPurchasesList() + "  <------->  " + purchaseToken);
                                }
                            };
                            mBillingClient.consumeAsync(sourcePurchase.getPurchaseToken(), listener);
                        } else {

                        }
                    }

                    if (purchasesResult.getPurchasesList().size() > 0) {
                        //  Log.d("anupam3", purchasesResult.getPurchasesList().size() + "");

                    } else {
                        //  Log.d("anupam4", purchasesResult.getPurchasesList().size() + "");
                        BillingFlowParams.Builder builder = BillingFlowParams.newBuilder().setSku("234r23446").setType(BillingClient.SkuType.SUBS);
                        int responseCode = mBillingClient.launchBillingFlow(SplashActivity.this, builder.build());
                        if (responseCode == 7) {

                            //Item already purchased
                        }
                    }
                }
            }

            @Override
            public void onBillingServiceDisconnected() {
                // Try to restart the connection on the next request to
                // Google Play by calling the startConnection() method.
                Toast.makeText(SplashActivity.this, "Failed", Toast.LENGTH_LONG).show();
            }
        });