Android billing api onBillingSetupFinished possible states

大憨熊 提交于 2021-01-29 15:31:22

问题


Which states on onBillingSetupFinished should I handle?

What I mean is, there are 12 possible states on the BillingResponseCode interface:

int SERVICE_TIMEOUT = -3;
int FEATURE_NOT_SUPPORTED = -2;
int SERVICE_DISCONNECTED = -1;
int OK = 0;
int USER_CANCELED = 1;
int SERVICE_UNAVAILABLE = 2;
int BILLING_UNAVAILABLE = 3;
int ITEM_UNAVAILABLE = 4;
int DEVELOPER_ERROR = 5;
int ERROR = 6;
int ITEM_ALREADY_OWNED = 7;
int ITEM_NOT_OWNED = 8;

But I imagine only a few of them are really send to this method, so which of them I should care?

public void onBillingSetupFinished(@NonNull BillingResult billingResult)
{
  switch(billingResult.getResponseCode())
  {
    case BillingClient.BillingResponseCode.OK:
      break;
    case BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED:
      break;
    case BillingClient.BillingResponseCode.ITEM_NOT_OWNED:
      break;
    case BillingClient.BillingResponseCode.BILLING_UNAVAILABLE:
      break;
    case BillingClient.BillingResponseCode.DEVELOPER_ERROR:
      break;
    ...
    default:
      
  }
}

Would help if I find the source code for the billing api, I couldn't find it.


回答1:


This is the google's official billing sample project. Hope this answers your question.




回答2:


I wrote this BillingClientListener based on Google's official ClassyTaxi sample app, that I am also using in production:

class BillingClientListener(
    private val billingClient: BillingClient,
    private val onBillingSetupOk: (() -> Unit)? = null
) : BillingClientStateListener {

    private var retryCount = 0
    override fun onBillingSetupFinished(billingResult: BillingResult) {
        Log.d(TAG, "Setup finished.")
        retryCount = 0
        if (billingResult.responseCode != OK) {
            Log.e(TAG, "Problem setting up in-app billing: " + billingResult.debugMessage)
            return
        }
        Log.d(TAG, "Setup successful. Querying inventory.")
        onBillingSetupOk?.invoke()

    }

    override fun onBillingServiceDisconnected() {
        // Try to restart the connection on the next request to
        // Google Play by calling the startConnection() method.
        retryCount++
        if (retryCount <= 3)
            billingClient.startConnection(this)

            // show error message TODO
    }
}

You basically only check if the setup was successful, meaning responseCode == OK, or not. Of course you can check for more, e.g for service timeout, but its not necessary. I can recommend looking at the complete example app, because it works pretty well.



来源:https://stackoverflow.com/questions/63161502/android-billing-api-onbillingsetupfinished-possible-states

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!