未调用应用内结算 onPurchaseUpdated

in-app billing onPurchaseUpdated not called

我正在测试一个应用程序的应用程序内结算我在 google 游戏控制台中设置了我的帐户并且一切正常并且购买成功但是最后一种方法 onPurchasesUpdated 我不知道是否它调用或不调用,但我没有得到任何吐司,所以也许它不调用我的代码有问题吗?该应用程序告诉我购买成功我如何在购买成功时编码一些东西?

public class MainActivity extends AppCompatActivity implements PurchasesUpdatedListener {

BillingClient mBillingClient;

private Button mBtnBuy;

private String product = "product_test_4";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mBtnBuy = findViewById(R.id.mBtn_buy);

    mBtnBuy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mBillingClient = BillingClient.newBuilder(MainActivity.this).setListener(new PurchasesUpdatedListener() {
                @Override
                public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {



                }


            }).build();

            mBillingClient.startConnection(new BillingClientStateListener() {
                @Override
                public void onBillingSetupFinished(int responseCode) {
                    startPurchase();
                }

                @Override
                public void onBillingServiceDisconnected() {

                }
            });
        }
    });


}

public void startPurchase() {

    List<String> skuList = new ArrayList<>();
    skuList.add(product);
    SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
    params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
    mBillingClient.querySkuDetailsAsync(params.build(),
            new SkuDetailsResponseListener() {
                @Override
                public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) {
                    if (responseCode == BillingClient.BillingResponse.OK
                            && skuDetailsList != null) {
                        for (SkuDetails skuDetails : skuDetailsList) {
                            String sku = skuDetails.getSku();
                            String price = skuDetails.getPrice();
                            if (product.equals(sku)) {

                            }
                        }
                    }
                }
                // Process the result.
            });

    BillingFlowParams flowParams = BillingFlowParams.newBuilder()
            .setSku(product)
            .setType(BillingClient.SkuType.INAPP) // SkuType.SUB for subscription
            .build();
    int responseCode = mBillingClient.launchBillingFlow(MainActivity.this, flowParams);
}


@Override
public void onPurchasesUpdated(@BillingClient.BillingResponse int responseCode, List<Purchase> purchases) {
    if (responseCode == BillingClient.BillingResponse.OK
            && purchases != null) {
        Toast.makeText(this, "purchase success", Toast.LENGTH_SHORT).show();
        Log.d("tag","purchase success");
    } else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
        // Handle an error caused by a user cancelling the purchase flow.
        Toast.makeText(this, "purchase cancelled", Toast.LENGTH_SHORT).show();
        Log.d("tag","purchase cancelled");
    } else {
        // Handle any other error codes.
        Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
        Log.d("tag","purchase error");
    }
}

}

您正在 mBtnBuy onClick 中设置 PurchasesUpdatedListener

mBillingClient = BillingClient.newBuilder(MainActivity.this).setListener(new PurchasesUpdatedListener() {
            @Override
            public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {



            }


        }).build();

以便在购买时调用该方法而不是最后一个 onPurchasesUpdated

你可以这样 mBillingClient = BillingClient.newBuilder(MainActivity.this).setListener().build(); 然后最后一个 onPurchasesUpdated 将被调用