android - In-app billing query inventory in AsyncTask? -
i followed this tutorial add loading screen launch of app while data loaded asynctask's doinbackground() function.
my app features in-app billing premium upgrade , query inventory check on launch. iabhelper
functions asynchronous.
how can integrate iabhelper
checks doinbackground() main activity loaded when has completed?
my billing code follows:
private void checkforpremiumpurchase() { billinghelper = new iabhelper(this, constants.base_64_key); //start setup. asynchronous , specified listener called once setup completes. billinghelper.startsetup(new iabhelper.oniabsetupfinishedlistener() { public void oniabsetupfinished(iabresult result) { if(result.issuccess()) { billinghelper.queryinventoryasync(mgotinventorylistener); } } }); } //listener that's called when finish querying items , subscriptions own iabhelper.queryinventoryfinishedlistener mgotinventorylistener = new iabhelper.queryinventoryfinishedlistener() { @override public void onqueryinventoryfinished(iabresult result, inventory inventory) { if(result.issuccess()) { ispremium = inventory.haspurchase(constants.sku_premium); log.d(constants.tag, "app " + (ispremium ? "premium" : "not premium")); } } };
asynctask
useful, , helps put long-running jobs onto background thread, , give nice clean mechanism updating ui before, during, , after background task runs ... without messing threads directly.
some other android apis, however, setup let initiate calls on main (ui) thread, , behind-the-scenes, work on background thread (they using asynctask
, although don't care).
in case, iabhelper
methods you're using asynchronous, , allow initiate them main thread, without blocking ui.
therefore, there no need run them in same asynctask#doinbackground()
method you're using other work, work off main thread.
i see 2 options:
1) concurrent loading / iab request
ideally, if need load data @ startup (and using asynctask
this), also kick off in-app billing request @ same time.
you describe main activity, i'm assuming app starts splash activity of kind (?). in splash activity, use:
public void oncreate(bundle savedinstancestate) { new myloadingasynctask().execute(); checkforpremiumpurchase(); }
and launch main activity when iab check completes:
public void onqueryinventoryfinished(iabresult result, inventory inventory) { ispremium = false; if(result.issuccess()) { ispremium = inventory.haspurchase(constants.sku_premium); log.d(constants.tag, "app " + (ispremium ? "premium" : "not premium")); } intent = new intent(self, mainactivity.class); i.putextra("ispremium", ispremium); startactivity(i); }
this assumes network iab transaction take longer app's normal "loading" process. (post comment if assumption isn't valid, , i'll handle case)
2) serialized loading, iab
if there's else app's design requires "finish loading" , then initiate iab request, can call checkforpremiumpurchase()
when asynctask
finishes work:
protected void onpostexecute(long result) { checkforpremiumpurchase(); }
asynctask#onpostexecute()
gets called on main thread when loading completes, , checkforpremiumpurchase()
safe call on main thread.
comment
in general, i'd recommend against delaying startup of app check premium upgrade. ideally, you'd find clever way save state (premium purchased) once, , avoid future checks. users appreciate that.
however, don't know difference between free / premium app, , whether difference shows ... so, maybe can't avoid.
Comments
Post a Comment