Android 在应用程序购买中 - 无法从 onBillingSetupFinished() 事件调用按钮 setText()
Android In App Purchase - Cannot call button setText() from onBillingSetupFinished() event
我有一个应用程序内购买功能,我认为我正在遵循标准流程,但遇到了与按钮 setText() 更新相关的奇怪问题。
这是我想要做的:
- 开始计费连接
- 连接成功/
onBillingSetupFinished()
我加载 SKus 并希望在按钮中显示价格。
这并不是一直有效,调试起来很痛苦,因为 Toast.makeText() 方法没有显示任何进度。但最终我可以在登录 Google 在模拟器中播放后在模拟器中重现该问题。
我注意到,如果我从 onBillingSetupFinished()
执行按钮 setText(price)
,则下一行代码不会执行。我也没有在 logcat 中看到任何内容。它在 BillingClientStateListener-Setting Hard code price
之后卡住了,从未达到 BillingClientStateListener-DONE Setting Hard code price
奇怪的是偶尔这行得通,但大多数时候行不通。有什么指示可以指导我下一步该做什么吗?
这是我的代码摘录:
布局摘录自:
<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");
}
});
我有一个应用程序内购买功能,我认为我正在遵循标准流程,但遇到了与按钮 setText() 更新相关的奇怪问题。 这是我想要做的:
- 开始计费连接
- 连接成功/
onBillingSetupFinished()
我加载 SKus 并希望在按钮中显示价格。 这并不是一直有效,调试起来很痛苦,因为 Toast.makeText() 方法没有显示任何进度。但最终我可以在登录 Google 在模拟器中播放后在模拟器中重现该问题。
我注意到,如果我从 onBillingSetupFinished()
执行按钮 setText(price)
,则下一行代码不会执行。我也没有在 logcat 中看到任何内容。它在 BillingClientStateListener-Setting Hard code price
之后卡住了,从未达到 BillingClientStateListener-DONE Setting Hard code price
奇怪的是偶尔这行得通,但大多数时候行不通。有什么指示可以指导我下一步该做什么吗?
这是我的代码摘录:
布局摘录自:
<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");
}
});