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

Ami*_*wal 2 android settext android-asynctask android-inapp-purchase

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

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

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

\n

这是我的代码摘录:

\n

布局摘录自:

\n
 <LinearLayout\n            android:layout_width="wrap_content"\n            android:orientation="horizontal"\n            android:layout_height="wrap_content"\n            android:layout_marginTop="2dp"\n            android:layout_gravity="center"\n            android:gravity="center_horizontal">\n              <Button\n                  android:id="@+id/btn_buy_100coins"\n                  style="@style/BuyButton250"\n                  android:layout_width="120dp"\n                  android:text="@string/buy"\n                  android:onClick="onClickBuy100"\n                  />\n            <Button\n                android:id="@+id/btn_buy_250coins"\n                style="@style/BuyButton250"\n                android:layout_width="120dp"\n                android:text="@string/buy"\n                android:layout_marginLeft="15dp"\n                android:onClick="onClickBuy250"/>\n\n            <Button\n                android:id="@+id/btn_buy_700coins"\n                style="@style/BuyButton250"\n                android:layout_width="120dp"\n                android:text="@string/buy"\n                android:layout_marginLeft="15dp"\n                android:onClick="onClickBuy700"/>\n            <TextView\n                android:layout_width="50dp"\n                android:layout_height="wrap_content"\n                android:layout_gravity="center"\n                android:text="@string/best_deal"\n                android:textColor="@color/blue_neon"\n                android:layout_marginLeft="5dp"\n                android:textSize="14sp"\n                android:textStyle="bold"\n                />\n\n        </LinearLayout>\n
Run Code Online (Sandbox Code Playgroud)\n

Java代码

\n
 @Override\n    protected void onCreate( Bundle savedInstanceState) {\n\n        super.onCreate(savedInstanceState);\n       \n        setContentView(R.layout.activity_earn_coins);\n\n\n        billingClient = BillingClient.newBuilder(this)\n                .setListener(purchasesUpdatedListener)\n                .enablePendingPurchases()\n                .build();\n\n\n        btn100 = findViewById(R.id.btn_buy_100coins);\n        btn250 = findViewById(R.id.btn_buy_250coins);\n        btn700 = findViewById(R.id.btn_buy_700coins);\n\n        //-->Setting Dummy price OnCreate - works OK\n        Log.i("BillingInfo-", "OnCreate: Setting Hard code price");\n        btn100.setText("\xe2\x82\xb910.00");\n        btn250.setText("\xe2\x82\xb920.00");\n        btn700.setText("\xe2\x82\xb930.00");\n        Log.i("BillingInfo-", " OnCreate: DONE Setting Hard code price");\n        //--<--\n\n         StartBillingConnection();\n   \n\n    } \n\n\n\n\nprivate void StartBillingConnection() {\n\n        Log.i("BillingInfo-", "Billing Client Starting");\n\n        billingClient.startConnection(new BillingClientStateListener() {\n            @Override\n            public void onBillingSetupFinished(BillingResult billingResult) {\n\n                int responseCode = billingResult.getResponseCode();\n                if (responseCode == BillingClient.BillingResponseCode.OK) {\n\n                    Log.i("BillingInfo-", "Billing Client connected");\n                  //  Toast.makeText(getApplicationContext(), "Billing client loaded successfully!", Toast.LENGTH_SHORT).show();\n\n                    //-->Setting price here - DOESN'T WORK, it goes in a weird state after the first setText(), though sometimes all 3 setText() work OK.\n                    Log.i("BillingInfo-", "BillingClientStateListener-Setting Hard code price");\n                    btn100.setText("\xe2\x82\xb940.00");\n                    btn250.setText("\xe2\x82\xb950.00");\n                    btn700.setText("\xe2\x82\xb960.00");\n                    Log.i("BillingInfo-", "BillingClientStateListener- DONE Setting Hard code price");\n                    //--<--\n\n\n                    // The BillingClient is ready. \n//Temporarily commented LoadSKUs as the issue is reprouced by calling button setText() from this method itself\n                    //LoadSKUsFromPlayConsole();\n                }\n                else{\n                    Log.i("BillingInfo-", "Billing Client connection failed");\n                 //   Toast.makeText(getApplicationContext(), "SKU Load failed! - Response Code:" + String.valueOf(responseCode), Toast.LENGTH_SHORT).show();\n\n                }\n            }\n\n            @Override\n            public void onBillingServiceDisconnected() {\n                // Try to restart the connection on the next request to\n                // Google Play by calling the startConnection() method.\n                Log.i("BillingInfo-", "Service Disconnected");\n                StartBillingConnection();\n            }\n        });\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

日志猫

\n
2021-08-13 12:42:44.290 590-590/com.kingsprolabs.puzzlify I/BillingInfo-: OnCreate: Setting Hard code price\n2021-08-13 12:42:44.290 590-590/com.kingsprolabs.puzzlify I/BillingInfo-:  OnCreate: DONE Setting Hard code price\n2021-08-13 12:42:44.291 590-590/com.kingsprolabs.puzzlify I/BillingInfo-: Billing Client Starting\n2021-08-13 12:42:45.105 590-780/com.kingsprolabs.puzzlify I/BillingInfo-: Billing Client connected\n2021-08-13 12:42:45.106 590-780/com.kingsprolabs.puzzlify I/BillingInfo-: BillingClientStateListener-Setting Hard code price\n
Run Code Online (Sandbox Code Playgroud)\n

Kam*_*yan 5

您需要从主线程更新 UI。

\n

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

\n
        requireActivity().runOnUiThread(new Runnable() {\n            @Override\n            public void run() {\n                btn100.setText("\xe2\x82\xb940.00");\n                btn250.setText("\xe2\x82\xb950.00");\n                btn700.setText("\xe2\x82\xb960.00");\n            }\n        });\n   \n
Run Code Online (Sandbox Code Playgroud)\n