android - mapping from asynctask to intent service -
i have android app widget have used async task network calls. want shift calls asynctask intent service. doinbackground() 's work can done in onhandleintent() onpreexecute() , onpostexecute(). have progress bar code in these 2 methods give spinning effect refresh button. should put code in intent service?
update
public class storewidgetprovider extends appwidgetprovider { @override public void onupdate(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids) { super.onupdate(context, appwidgetmanager, appwidgetids); updateviews = new remoteviews(context.getpackagename(), r.layout.widget_main_layout1); mcontext = context; mprogressbar = new progressbar(context); intent localintent = new intent(context,storewidgetservice.class); context.startservice(localintent); // try { // fetchtask.execute().get(); // } catch (interruptedexception e) { // // todo auto-generated catch block // e.printstacktrace(); // } catch (executionexception e) { // // todo auto-generated catch block // e.printstacktrace(); // } mycurrentlocation(mcontext); imageloader = new imageloader(mcontext); updateviews.setonclickpendingintent(r.id.next, next_buildbuttonpendingintent(context)); updateviews.setonclickpendingintent(r.id.back, back_buildbuttonpendingintent(context)); updateviews.setonclickpendingintent(r.id.refresh, refresh_buildbuttonpendingintent(context)); //----these commented use context , gets null when killed---- //updateviews.setonclickpendingintent(r.id.outer_text_view, merchant_buildbuttonpendingintent(context)); //updateviews.setonclickpendingintent(r.id.check_in, checkin_buildbuttonpendingintent(context)); //updateviews.setonclickpendingintent(r.id.image_logo_id, picon_buildbuttonpendingintent(context)); pushwidgetupdate(context,updateviews); } //my button listeners: next , prev //--not shown here--- public static void pushwidgetupdate(context context,remoteviews views){ system.out.println("inside pushwidget"); context = mcontext; if(context!=null){ componentname mywidget=new componentname(context, storewidgetprovider.class); appwidgetmanager manager=appwidgetmanager.getinstance(context); manager.updateappwidget(mywidget, views); } } } public static class storewidgetservice extends intentservice implements serverrequestenvironment{ public storewidgetservice() { super("storewidgetservice"); // todo auto-generated constructor stub } // protected void onpreexecute(){ // // updateviews.setviewvisibility(r.id.refresh, view.gone); // updateviews.setviewvisibility(r.id.progress, view.visible); // pushwidgetupdate(mcontext,updateviews); // } @override public int onstartcommand(intent intent, int flags, int startid){ super.onstartcommand(intent, flags, startid); return start_redeliver_intent; } @override public void onhandleintent(intent intent) { //my data fetching done network calls } // protected void onpostexecute(store storeobj) { // // updateviews.setviewvisibility(r.id.progress, view.gone); // updateviews.setviewvisibility(r.id.refresh, view.visible); // // // pushwidgetupdate(mcontext,updateviews); // } @override public ibinder onbind(intent arg0) { // todo auto-generated method stub return null; } } }
you have register handler
in main ui thread, pass handler
intentservice
. through handler can queue runnable
s execution on ui thread. since asynctask
backed threadpool
, think asynctask
indeed doing.
you can try use function runonuithread()
(but think can called activity
object).
take here deeper understanding:
http://developer.android.com/training/multiple-threads/communicate-ui.html
Comments
Post a Comment