android - Using AsyncTask with timer in fragment -


i'm using asynctask load data via volley. want asynctask invoked after n minutes, maybe duplicate , silly question, cant figure how implement it. maybe need implement asynctask in fragment ?

this asynctask.class

package com.aa.qc.task;  import android.os.asynctask;  import com.aa.qc.callbacks.zlecenialoadedlistner; import com.aa.qc.extras.zleceniasorter; import com.aa.qc.extras.zleceniautils; import com.aa.qc.network.volleysingleton; import com.aa.qc.pojo.zlecenia; import com.android.volley.requestqueue;  import java.util.arraylist;   public class taskloadzlecenia extends asynctask<void, void, arraylist<zlecenia>>{      private zlecenialoadedlistner mycomponent;     private volleysingleton volleysingleton;     private requestqueue requestqueue;     private zleceniasorter msorter = new zleceniasorter();       public taskloadzlecenia(zlecenialoadedlistner mycomponent) {         this.mycomponent = mycomponent;         volleysingleton = volleysingleton.getsinstance();         requestqueue = volleysingleton.getmrequestqueue();      }       @override     protected arraylist<zlecenia> doinbackground(void... params) {          arraylist<zlecenia> listzlecenias = zleceniautils.loadzlecenia(requestqueue);         msorter.sortzleceniabytime(listzlecenias);         return listzlecenias;     }      @override     protected void onpostexecute(arraylist<zlecenia> listzlecenias) {         if (mycomponent != null) {             mycomponent.onzlecenialoaded(listzlecenias);          }     }  } 

this listner.class

package com.aa.qc.callbacks;  import com.aa.qc.pojo.zlecenia;  import java.util.arraylist;  public interface zlecenialoadedlistner {     public void onzlecenialoaded(arraylist<zlecenia> listzlecenias); } 

this fragment want repeat asynctask (i have activity 2 tabs, each tabs fragment)

public class fragmentzlecenia extends fragment implements sortlistener, zlecenialoadedlistner, swiperefreshlayout.onrefreshlistener {      // todo: rename parameter arguments, choose names match     // fragment initialization parameters, e.g. arg_item_number     private static final string arg_param1 = "param1";     private static final string arg_param2 = "param2";      private arraylist<zlecenia> listzlecenias = new arraylist<>();     private recyclerview zleceniaarrived;     private adapterzlecenia adapterzlecenia;      private swiperefreshlayout mswiperefreshlayout;     private zleceniasorter msorter = new zleceniasorter();       // todo: rename , change types of parameters     private string mparam1;     private string mparam2;       /**      * use factory method create new instance of      * fragment using provided parameters.      *      * @param param1 parameter 1.      * @param param2 parameter 2.      * @return new instance of fragment fragmentzlecenia.      */     // todo: rename , change types , number of parameters     public static fragmentzlecenia newinstance(string param1, string param2) {         fragmentzlecenia fragment = new fragmentzlecenia();         bundle args = new bundle();         args.putstring(arg_param1, param1);         args.putstring(arg_param2, param2);         fragment.setarguments(args);         return fragment;     }      @override     public void onsaveinstancestate(bundle outstate) {         super.onsaveinstancestate(outstate);     }      public fragmentzlecenia() {         // required empty public constructor     }       @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         if (getarguments() != null) {             mparam1 = getarguments().getstring(arg_param1);             mparam2 = getarguments().getstring(arg_param2);         }     }      public void onsortbytime() {         l.t(getactivity(), "sort time");         msorter.sortzleceniabytime(listzlecenias);         adapterzlecenia.notifydatasetchanged();     }       @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         // inflate layout fragment          view layout = inflater.inflate(r.layout.fragment_zlecenia, container, false);          mswiperefreshlayout = (swiperefreshlayout) layout.findviewbyid(r.id.swipezlecenia);         mswiperefreshlayout.setonrefreshlistener(this);           zleceniaarrived = (recyclerview) layout.findviewbyid(r.id.listzlecenia);         zleceniaarrived.setlayoutmanager(new linearlayoutmanager(getactivity()));          adapterzlecenia = new adapterzlecenia(getactivity());         zleceniaarrived.setadapter(adapterzlecenia);          if (savedinstancestate != null) {             //if fragment starts after rotation or configuration change, load existing zlecenias db             listzlecenias = myapplication.getwritabledatabase().getallzlecenia();         } else {             //if fragment starts first time, load list of zlecenias database             myapplication.getwritabledatabase().deleteall();             //if database empty, trigger asycntask download zlecenias list web             if (listzlecenias.isempty()) {                 new taskloadzlecenia(this).execute();             }         }         //update adapter containg retrieved zlecenias         adapterzlecenia.setlistzlecenia(listzlecenias);          return layout;     }       @override     public void onzlecenialoaded(arraylist<zlecenia> listzlecenias) {          l.m("onzlecenialoaded fragment");          new taskloadzlecenia(this).execute();         adapterzlecenia.setlistzlecenia(listzlecenias);     }       @override     public void onrefresh() {         l.t(getactivity(), "onrefresh");         new taskloadzlecenia(this).execute();      }  } 

if want delay n seconds can use countdowntimer class this

 new countdowntimer(n * 1000, n * 1000) {       public void ontick(long millisuntilfinished) {       }       public void onfinish() {          //invokeallthethings!      }   }.start(); 

it make job :)


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -