I am using Google billing service, version 2.0.3.
Running the BillingClient.queryPurchases
method in the UI thread in the activity onstart method, users sometimes encounter Application Not Responding errors like this:
THREADS:
"main" prio=5 tid=1 TimedWaiting
| group="main" sCount=1 dsCount=0 obj=0x73b04268 self=0xa5905400
| sysTid=8394 nice=-4 cgrp=default sched=0/0 handle=0xa85ae534
| state=S schedstat=( 0 0 0 ) utm=2439 stm=491 core=1 HZ=100
| stack=0xbe437000-0xbe439000 stackSize=8MB
| held mutexes=
at java.lang.Object.wait! (Native method)
- waiting on <0x05482764> (a java.lang.Object)
at java.lang.Thread.parkFor$ (Thread.java:2127)
- locked <0x05482764> (a java.lang.Object)
at sun.misc.Unsafe.park (Unsafe.java:325)
at java.util.concurrent.locks.LockSupport.parkNanos (LockSupport.java:201)
at java.util.concurrent.FutureTask.awaitDone (FutureTask.java:418)
at java.util.concurrent.FutureTask.get (FutureTask.java:176)
at com.android.billingclient.api.BillingClientImpl.queryPurchases (BillingClientImpl.java:699)
What is the best way to fix this type of ANRs?
↳ com.android.billingclient.api.BillingClient. Main interface for communication between the library and user application code. It provides convenience methods for in-app billing. You can create one instance of this class for your application and use it to process in-app billing operations.
Google Play's billing system is a service that enables you to sell digital products and content in your Android app. You can use Google Play's billing system to sell a one-time product or subscriptions on a recurring basis.
To acknowledge non-consumable purchases, use either BillingClient. acknowledgePurchase() from the Google Play Billing Library or Product. Purchases. Acknowledge from the Google Play Developer API.
Use queryPurchasesAsync (String, PurchasesResponseListener) instead. Returns purchases details for currently owned items bought within your app. Only active subscriptions and non-consumed one-time purchases are returned. This method uses a cache of Google Play Store app without initiating a network request.
It provides synchronous (blocking) and asynchronous (non-blocking) methods for many common in-app billing operations. It's strongly recommended that you instantiate only one BillingClient instance at one time to avoid multiple PurchasesUpdatedListener.onPurchasesUpdated (BillingResult, List) callbacks for a single event.
Initiates the billing flow for an in-app purchase or subscription. Initiates a flow to confirm the change of price for an item subscribed by the user. Constructs a new BillingClient.Builder instance. Returns the most recent purchase made by the user for each SKU, even if that purchase is expired, canceled, or consumed. This method is deprecated.
To perform setup, call the startConnection (BillingClientStateListener) method and provide a listener; that listener will be notified when setup is complete, after which (and not before) you may start calling other methods. After setup is complete, you will typically want to request an inventory of owned items and subscriptions.
Since Version 4.0.0 of the Google Play Billing Library you can use BillingClient.queryPurchasesAsync()
.BillingClient.queryPurchases()
will be removed in a future release.
implementation("com.android.billingclient:billing:4.0.0")
billingClient.queryPurchasesAsync(BillingClient.SkuType.INAPP, (billingResult1, list) -> {
...
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With