java - start AsyncTask from one activity, get result in another -


i'm new android programming, , i'd create central database service class take care of user data exchange external database. this, created service started after successful login. created class extends asynctask data retrieval. now, wanted methods data retrieval stored in service. fire intents service different activities, , .setaction() determine method call, or data retrieve.

i created interface class handling asynctask results. now, this question thought possible have multiple listeners 1 , same asynctask result. seems impossible achieve: i'd retrieve asynctask results in mainmenuactivity, can't create instance of asyncuserdata there delegate userdata class. in example below, missing piece valid instance of asyncuserdata userdata class work with. how it?

here's example: mainmenuactivity

public class mainmenuactivity extends actionbaractivity implements asyncuserdata {   textview tvusername;    protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main_menu);     tvusername =       (textview) findviewbyid(r.id.tvusername);     telephonymanager tmanager = (telephonymanager) this.getsystemservice(context.telephony_service);     string uid = tmanager.getdeviceid();     getdatafromusersessionservice(this, uid);   }    @override   public boolean oncreateoptionsmenu(menu menu) {     getmenuinflater().inflate(r.menu.menu_main_menu, menu);     return true;   }    @override   public boolean onoptionsitemselected(menuitem item) {     int id = item.getitemid();     if (id == r.id.action_settings) {       return true;     }     return super.onoptionsitemselected(item);   }    @override   public void retrieveresult(string result) throws jsonexception {     jsonobject jsonobject = new jsonobject(result);     string joname;     joname = jsonobject.getjsonobject("name").tostring();     user.setname(joname);     tvusername.settext(joname);   }    public void getdatafromusersessionservice(context context, string uid) {     intent intent = new intent(context, usersession.class);     intent.setaction(usersession.action_fetch_user_data);     intent.putextra(usersession.uid, uid);     context.startservice(intent);   } 

usersession service

public class usersession extends intentservice {   public static final string action_fetch_user_data = "com.example.blahblah.services.action.read_user_data";    @override   protected void onhandleintent(intent intent) {     if (intent != null) {       utils = new utils(this);       final string action = intent.getaction();       uid = intent.getstringextra(uid);       if (action_fetch_user_data.equals(action)) {         handleuserdatafetch(uid);       }     }   }    private void handleuserdatafetch(string uid) {     string[] parameters = new string[2];     parameters[0] = uid;     parameters[1] = constants.user_data_fetch;     userdata userdata = new userdata(this);     userdata.execute(parameters);   } 

userdata asynctask class (the utils class has post method):

public class userdata extends asynctask < string, void, string > {   public asyncuserdata delegate = null;   private context mycontext;    public userdata(context context) {     mycontext = context;   }    @override   protected string doinbackground(string...params) {     string serverresponse = "";     string uid = params[0];     utils utils = new utils(mycontext);     string phpname = params[1];     list < namevaluepair > namevaluepairs = new arraylist < namevaluepair > ();     namevaluepairs.add(new basicnamevaluepair("uid", uid));      try {        serverresponse = utils.passdatatoserver(phpname, namevaluepairs);     } catch (ioexception e) {       e.printstacktrace();     }     return serverresponse;   }    protected void onpostexecute(string result) {     try {       delegate.retrieveresult(result);     } catch (jsonexception e) {       e.printstacktrace();     }   }  }; 

and asyncuserdata interface:

public interface asyncuserdata {   void retrieveresult(string result) throws jsonexception; } 

you can use singleton stores reference activity

    public class servicetoactivity     {                  public actionbaractivity mainactivity = null;                 private static servicetoactivity singleton = null;                 public class<?> cl = null;                  private servicetoactivity()                 {                  }                  public static actionbaractivity getsingleton()                 {                     if(singleton==null)                            return null;                     return singleton.mainactivity;                 }                 public static class<?> getsingletonclass()                 {                     if(singleton==null)                            return null;                     return singleton.cl;                 }                  public static void setsingleton(actionbaractivity mainactivity, class<?> cl)                 {                       if(singleton==null)                              singleton = new servicetoactivity();                       singleton.mainactivity = mainactivity;                       singleton.cl = cl;                 }     } 

then create singleton before service started

  public void getdatafromusersessionservice(context context, string uid) { intent intent = new intent(context, usersession.class); intent.setaction(usersession.action_fetch_user_data); intent.putextra(usersession.uid, uid); servicetoactivity.setsingleton(this,this.getclass());   //create singleton store reference activity context.startservice(intent); } 

in userdata retrieve data main activity by:

 protected void onpostexecute(string result) { try {   class<?> cl = servicetoactivity.getsingletonclass();   method met = cl.getmethod("retrieveresult", string); //string because result of type string: can use result.getclass() instead   met.invoke(cl.cast(servicetoactivity.getsingleton()), result); // compare servicetoactivity.getsingleton().retrieveresult(result); } catch (jsonexception e) {   e.printstacktrace(); } } 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -