java - Android background timer updating a Texview Item of Recyclerview -
i'm using handler create countdown timer inside of service class. timer works decreasing remaining time , works updating texview on item of recyclerview working. problem is, when application closed , reopen, timer no longer able update texview on item of recyclerview working.
from recyclerview adapter i'm calling function service start timer. , of eventbus libray able change texview.
... @override public pageadapter.theviewholder oncreateviewholder(viewgroup parent, int viewtype) { if (!eventbus.getdefault().isregistered(this)) { //register eventbus library eventbus.getdefault().register(this); } view itemview = layoutinflater.from(parent.getcontext()).inflate(r.layout.custom_item, parent, false); return new theviewholder(itemview); } @override public void onbindviewholder(final pageadapter.theviewholder holder, int position) { holder.btn_start_countdown.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { new servicehelper().starttimer(milliseconds, holder);//call function in service start timer } } } //the pojo eventbus post new data public static class countertextpojo { volatile private long duration; private theviewholder holder; public countertextpojo(long duration, theviewholder holder){ this.duration = duration; this.holder = holder; } public long getduration(){ return duration; } } //eventbus function change textview duration @subscribe(threadmode = threadmode.main) public void onevent(countertextpojo data) { long duration = data.getduration(); theviewholder holder = data.getholder(); int seconds = (int) (duration / 1000) % 60 ; int minutes = (int) ((duration / (1000*60)) % 60); int hours = (int) ((duration / (1000*60*60)) % 24); holder.durationtexview.settext(string.format("%02d:%02d:%02d",hours,minutes,seconds)); if(duration < 1){ //countdown finish... staf } }
this part of service class timer called.
static handler handler=new handler(); ... public void starttimer(long duration, pageadapter.theviewholder holder){ ... handler.postdelayed(new timer(duration, holder),0);//call class start timer } static class timer implements runnable { volatile pageadapter.theviewholder holder; volatile long duration; public timer(long duration, pageadapter.theviewholder holder){ this.holder = holder; this.duration = duration; } @override public void run() { ... duration -= 1000; //remove 1 seconds handler.postdelayed(this,1000); //delay in 1 seconds eventbus.getdefault().post(new pageadapter.countertextpojo(duration, holder)); //update textview using eventbus function } }
what possible way achieve want?
Comments
Post a Comment