Android billing - how do I invoke the onUpgradeAppButtonClicked() method if it is not from a button click?

StackOverflow https://stackoverflow.com/questions/16153843

  •  11-04-2022
  •  | 
  •  

Question

I am creating the subscription purchase for an app. And I originally had something like this:

    // CREATE THE SUBSCRIBE BUTTON
    Button subscribe = (Button)findViewById(R.id.subscribe);
    subscribe.setOnClickListener(new Button.OnClickListener() 
    {  
      public void onClick(View v) 
      {             
          onUpgradeAppButtonClicked ( );
      }
    });                   

public void onUpgradeAppButtonClicked( ) 
{
    Log.d(TAG, "Buy button clicked; launching purchase flow for upgrade.");
    setWaitScreen(true);
    mHelper.launchPurchaseFlow(this, SKU_SUBSCRIPTION, RC_REQUEST, mPurchaseFinishedListener);
}

But then I read that the IABHelper needs to get set asynchronously. And that invoking this method from a button click might not be right.

But how does it possibly get invoked if not from a button click?

Thanks!

Was it helpful?

Solution

I suppose what was meant is, that it will take some time, so you should run it asynchronously, so it does not freeze your UI. So, sure you'll invoke it from a button click, but not directly. Instead, in your onUpgradeAppButtonClicked() method, do something to run the mHelper.launchPurchaseFlow()... asynchronously, like using an AsyncTask or a separate Thread or the like.

The famous Painless Threading Article comes, as always, very handy for this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top