应用内结算onPurchaseUpdated未调用

时间:2018-12-27 11:03:13

标签: android in-app-purchase in-app-billing

我正在测试一个应用程序内结算应用程序,我在Google Play控制台中设置了帐户,并且一切正常,并且购买成功,但是最后一种方法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");
    }
}

}

1 个答案:

答案 0 :(得分:0)

您正在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