android - i want TimerTask pause and resume -
i working in game have need use time update in every second.i using timertask.i want pause time when clicked on button , want resume again when clicked on resume button other activity.how please me.
t=new timer(); { t.scheduleatfixedrate(new timertask() { public void run() { runonuithread(new runnable() { @override public void run() { textview tv = (textview) findviewbyid(r.id.time); tv.settext(string.format("%02d:%02d",minute,seconds)); time += 1; seconds += 1; if(seconds==60) { seconds=0; } minute=time/60; } }); } }, 0, 1000); }
try this want copy , paste public class mainactivity extends activity { edittext t; button b; button b2; timer timer; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); b=(button)findviewbyid(r.id.button1); b2=(button)findviewbyid(r.id.button2); t=(edittext)findviewbyid(r.id.edittext1); b.setbackgroundresource(android.r.color.holo_green_dark); timer = new timer(); timertask updateprofile = new customtimertask(mainactivity.this); timer.scheduleatfixedrate(updateprofile, 0,1000); b.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub timer.cancel(); } }); b2.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub timer = new timer(); timertask updateprofile = new customtimertask(mainactivity.this); timer.scheduleatfixedrate(updateprofile, 0,1000); } }); } class customtimertask extends timertask { private context context; private handler mhandler = new handler(); // write custom constructor pass context public customtimertask(context con) { this.context = con; } @override public void run() { new thread(new runnable() { @override public void run() { mhandler.post(new runnable() { @override public void run() { calendar c =calendar.getinstance(); int seconds = c.get(calendar.second); int min= c.get(calendar.minute); int hour= c.get(calendar.hour); t.settext(hour+":"+min+":"+seconds); } }); } }).start(); } } }
Comments
Post a Comment