I'm using a google provided code (IabHelper.java, etc.) to setup in-App billing in my Android App. The provided code is asynchronous in the sense that I need to specify a callback function that will fun upon successfully binding a service.
However, I found myself in a need to possess information about current subscriptions across different activities. So I decided to bind the billing service synchronously in onCreate method of the Activity and keep it in a static variable. This should be relatively fast since Google Play Services do some caching. The code I use is the following:
// this - current activity
CountDownLatch latch = new CountDownLatch(1);
mService = new BillingService(this, latch);
try {
latch.await();
} catch (InterruptedException ignored) {}
And then in onIabSetupFinished
I call latch.countDown()
. Unfortunately, countDown
is never get executed and my code is stuck in await()
. Does anyone know how to solve this or suggest an alternative solution?
onIabSetupFinished()
is called from ServiceConnection.onServiceConnected()
which is scheduled for execution on the main thread of your app. It can't run though since you are blocking your main thread with latch.await()
.
You could not block the app and instead just display an uncancellable ProgressDialog
until you get the information from the IAB service.