I'm trying to follow the In-App Billing tutorial on the Android Developer site. I'm up to Making In-app Billing Requests. There is code I'm supposed to add to my project:
ArrayList<String> skuList = new ArrayList<String> ();
skuList.add("premiumUpgrade");
skuList.add("gas");
Bundle querySkus = new Bundle();
querySkus.putStringArrayList(“ITEM_ID_LIST”, skuList);
All well and good, but where do I put this code? In OnCreate()? Or somewhere else?
According to the same guide you are following, you must use that code to query if certain items are available for purchase. And you must call the method mService.getSkuDetails()
. Like this:
Bundle skuDetails = mService.getSkuDetails(3, getPackageName(), "inapp", querySkus);
The guide also reads:
Warning: Do not call the getSkuDetails method on the main thread. Calling this method triggers a network request which could block your main thread. Instead, create a separate thread and call the getSkuDetails method from inside that thread.
So, answering your question, your code must be put inside an AsyncTask
, preferably inside doInBackground()
method, because it implicates networking and cannot be done on the main thread.
To learn more about using an AsyncTask
, check this link.
Hope to have helpe you.