android 计费如何启用 enablePendingPurchases()

android billing how to enable enablePendingPurchases()

我已经从旧的 gradle 账单 api 转移到最新的账单,现在我尝试添加

  BillingClient.Builder enablePendingPurchases = BillingClient.newBuilder(this).setListener(this);

但我无法让它工作,这是错误

   Caused by: java.lang.IllegalArgumentException: Support for pending purchases must be enabled. Enable this by calling 'enablePendingPurchases()' on BillingClientBuilder.
        at com.android.billingclient.api.BillingClient$Builder.build(BillingClient.java:309)
        at com.aplicacion.vivaluganoapp.ar.ponerDineroActivity.setupBillingClient(ponerDineroActivity.java:144)
        at com.aplicacion.vivaluganoapp.ar.ponerDineroActivity.onCreate(ponerDineroActivity.java:125)

完整代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poner_dinero);

        recyclerProduct.setHasFixedSize(true);
        recyclerProduct.setLayoutManager(new LinearLayoutManager(this));
        BillingClient.Builder enablePendingPurchases = BillingClient.newBuilder(this).setListener(this);

 enablePendingPurchases.build();
setupBillingClient();
    }




    private void setupBillingClient() {


        billingClient = BillingClient.newBuilder (this).setListener(this).build();

        billingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(BillingResult responseCode) {
                int maca = BillingClient.BillingResponseCode.OK;
                String maca2 = String.valueOf(maca);

                String maca3 = String.valueOf(responseCode);
                if (maca3 == maca2)
                {
                    Toast.makeText(ponerDineroActivity.this, "WORKS", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(ponerDineroActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onBillingServiceDisconnected() {
                Toast.makeText(ponerDineroActivity.this, "Disconnected from Billing", Toast.LENGTH_SHORT).show();
            }
        });

    }

如果我只放置:

BillingClient.Builder enablePendingPurchases = BillingClient.newBuilder(this);

错误是:

Caused by: java.lang.IllegalArgumentException: Please provide a valid listener for purchases updates.

有什么帮助吗?我厌倦了尝试

来自您问题中的第一个堆栈跟踪

Enable this by calling 'enablePendingPurchases()'

我们可以找到方法的文档 enablePendingPurchases()

This method is required to be called to acknowledge your application has been updated to support purchases that are pending. Pending purchases are not automatically enabled since your application will require updates to ensure entitlement is not granted before payment has been secured. For more information on how to handle pending transactions see https://developer.android.com/google/play/billing/billing_library_overview

If this method is not called, BillingClient instance creation fails.

你的代码行应该是:-

enablePendingPurchases = BillingClient.newBuilder(this)
   .enablePendingPurchases()
   .setListener(this);

而不是:-

enablePendingPurchases = BillingClient.newBuilder(this).setListener(this);
BillingClient billingClient = 
BillingClient.newBuilder(context!!)
.enablePendingPurchases()
.setListener(this)
 build()
    billingClient.startConnection(object : BillingClientStateListener {
        override fun onBillingSetupFinished(billingResult: BillingResult) {
            if (billingResult.responseCode==BillingClient.BillingResponseCode.OK) {
                skuList =  HashMap()
                skuList.put(BillingClient.SkuType.SUBS, listOf(getString(R.string.subscription_monthly),getString(R.string.subscription_yearly)))

                querySkuDetailsAsync(BillingClient.SkuType.SUBS,skuList.get(BillingClient.SkuType.SUBS),object :SkuDetailsResponseListener{
                    override fun onSkuDetailsResponse(billingResult: BillingResult?, skuDetailsList: MutableList<SkuDetails>?) {

                        DebugLog.e("DATAAA "+skuDetailsList?.size+"")
                    }
                })
            }
        }
        override fun onBillingServiceDisconnected() {
            // Try to restart the connection on the next request to
            // Google Play by calling the startConnection() method.
        }
    })

这对我有用。

只需添加 enablePendingPurchases() 如下所示:

billingClient = BillingClient.newBuilder(this)
                             .setListener(this)
                             .enablePendingPurchases()
                             .build();