gpt4 book ai didi

Android 应用内购买 - 无法从 onBillingSetupFinished() 事件调用按钮 setText()

转载 作者:行者123 更新时间:2023-12-05 00:18:15 24 4
gpt4 key购买 nike

我有一个应用程序内购买功能,我认为遵循标准流程,但遇到了与按钮 setText() 更新相关的奇怪问题的挑战。这是我想做的:

  1. 启动结算连接
  2. 成功连接/onBillingSetupFinished() 我加载 SKus 并希望在按钮中显示价格。这并不总是有效,而且调试起来很痛苦,因为 Toast.makeText() 方法不显示任何进度。但最终我在模拟器中登录 Google Play 后可以在模拟器中重现该问题。

我注意到,如果我从 onBillingSetupFinished() 执行按钮 setText(price) ,则下一行代码不会执行。我在 logcat 中也没有看到任何内容。它在 BillingClientStateListener-Setting Hard codeprice 之后卡住,永远不会达到 BillingClientStateListener-DONESetting Hard codeprice奇怪的是,有时这会起作用,但大多数时候却不起作用。有什么指示可以指导我下一步应该做什么吗?

这是我的代码摘录:

布局摘录:

 <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 代码

 @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

最佳答案

您需要从主线程更新 UI。

解决方案代码:将其写入onBillingSetupFinished

        requireActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
btn100.setText("₹40.00");
btn250.setText("₹50.00");
btn700.setText("₹60.00");
}
});

关于Android 应用内购买 - 无法从 onBillingSetupFinished() 事件调用按钮 setText(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68768468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com