作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个应用程序内购买功能,我认为遵循标准流程,但遇到了与按钮 setText() 更新相关的奇怪问题的挑战。这是我想做的:
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/
我尝试使用 Kotlin Coroutines 为 BillingClient v.2.2.0 编写一个包装器: package com.cantalk.photopose.billing impor
我有一个应用程序内购买功能,我认为遵循标准流程,但遇到了与按钮 setText() 更新相关的奇怪问题的挑战。这是我想做的: 启动结算连接 成功连接/onBillingSetupFinished()
我是一名优秀的程序员,十分优秀!