I have an In App Purchase feature where I think am following the standard process but getting challenged with a weird issue related to a Button setText() updates. Here is what I want to do:
onBillingSetupFinished()
I Load the SKus and want to show the price in the buttons.
This isn't working all the time and it was pain to debug as Toast.makeText() methods isnt displaying any progress. But finally I could reproduce the issue in emulator after logging to Google Play in the emulator.What I noticed is if I do a Button setText(price)
from onBillingSetupFinished()
the next lines of code don't execute. I don't see anything in the logcat as well. It gets stuck after BillingClientStateListener-Setting Hard code price
, never reaches to BillingClientStateListener-DONE Setting Hard code price
Weird thing is occasionally this works but most times it doesn't. Any pointers to guide me what should I do next?
Here is my code excerpt:
Layout excerpt from:
<LinearLayout
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_gravity="center"
android:gravity="center_horizontal">
<Button
android:id="@+id/btn_buy_100coins"
style="@style/BuyButton250"
android:layout_width="120dp"
android:text="@string/buy"
android:onClick="onClickBuy100"
/>
<Button
android:id="@+id/btn_buy_250coins"
style="@style/BuyButton250"
android:layout_width="120dp"
android:text="@string/buy"
android:layout_marginLeft="15dp"
android:onClick="onClickBuy250"/>
<Button
android:id="@+id/btn_buy_700coins"
style="@style/BuyButton250"
android:layout_width="120dp"
android:text="@string/buy"
android:layout_marginLeft="15dp"
android:onClick="onClickBuy700"/>
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/best_deal"
android:textColor="@color/blue_neon"
android:layout_marginLeft="5dp"
android:textSize="14sp"
android:textStyle="bold"
/>
</LinearLayout>
Java Code
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_earn_coins);
billingClient = BillingClient.newBuilder(this)
.setListener(purchasesUpdatedListener)
.enablePendingPurchases()
.build();
btn100 = findViewById(R.id.btn_buy_100coins);
btn250 = findViewById(R.id.btn_buy_250coins);
btn700 = findViewById(R.id.btn_buy_700coins);
//-->Setting Dummy price OnCreate - works OK
Log.i("BillingInfo-", "OnCreate: Setting Hard code price");
btn100.setText("₹10.00");
btn250.setText("₹20.00");
btn700.setText("₹30.00");
Log.i("BillingInfo-", " OnCreate: DONE Setting Hard code price");
//--<--
StartBillingConnection();
}
private void StartBillingConnection() {
Log.i("BillingInfo-", "Billing Client Starting");
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
int responseCode = billingResult.getResponseCode();
if (responseCode == BillingClient.BillingResponseCode.OK) {
Log.i("BillingInfo-", "Billing Client connected");
// Toast.makeText(getApplicationContext(), "Billing client loaded successfully!", Toast.LENGTH_SHORT).show();
//-->Setting price here - DOESN'T WORK, it goes in a weird state after the first setText(), though sometimes all 3 setText() work OK.
Log.i("BillingInfo-", "BillingClientStateListener-Setting Hard code price");
btn100.setText("₹40.00");
btn250.setText("₹50.00");
btn700.setText("₹60.00");
Log.i("BillingInfo-", "BillingClientStateListener- DONE Setting Hard code price");
//--<--
// The BillingClient is ready.
//Temporarily commented LoadSKUs as the issue is reprouced by calling button setText() from this method itself
//LoadSKUsFromPlayConsole();
}
else{
Log.i("BillingInfo-", "Billing Client connection failed");
// Toast.makeText(getApplicationContext(), "SKU Load failed! - Response Code:" + String.valueOf(responseCode), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Log.i("BillingInfo-", "Service Disconnected");
StartBillingConnection();
}
});
}
LogCat
2021-08-13 12:42:44.290 590-590/com.kingsprolabs.puzzlify I/BillingInfo-: OnCreate: Setting Hard code price
2021-08-13 12:42:44.290 590-590/com.kingsprolabs.puzzlify I/BillingInfo-: OnCreate: DONE Setting Hard code price
2021-08-13 12:42:44.291 590-590/com.kingsprolabs.puzzlify I/BillingInfo-: Billing Client Starting
2021-08-13 12:42:45.105 590-780/com.kingsprolabs.puzzlify I/BillingInfo-: Billing Client connected
2021-08-13 12:42:45.106 590-780/com.kingsprolabs.puzzlify I/BillingInfo-: BillingClientStateListener-Setting Hard code price
You need to update UI from the main thread.
Solution Code: Write this inside the onBillingSetupFinished
requireActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
btn100.setText("₹40.00");
btn250.setText("₹50.00");
btn700.setText("₹60.00");
}
});