How to structure my Android timer app with thread? -
How to structure my Android timer app with thread? -
i'm trying create simple workout timer android. user creates workoutplan
containing info such total duration, total rest time, etc, app display timer updates every second.
pretty simple begin with, i'm trying build app possible, i.e. separation of concern, right techniques, responsive ui, ensure timer accurate, etc.
here's simplification of have far:
workoutplan
, user can load/save different workout plans timer
public class workoutplan { public long duration; }
timerthread
, class implements runnable
, takes in workoutplan
in constructor, start/stop, shows elapsed time.
public class timerthread implements runnable { public boolean running = false; private long laststarttime; private long savedtime; private workoutplan plan; private handler handler; public timerthread(workoutplan p, handler h) { plan = p; handler = h; } public synchronized void start() { laststarttime = system.currenttimemillis(); running = true; } public synchronized void pause() { savedtime = elapsedtime(); laststarttime = 0; running = false; } public synchronized long elapsedtime() { if (laststarttime == 0) { homecoming savedtime; } else { homecoming savedtime + (system.currenttimemillis() - laststarttime); } } public synchronized string currtimestr() { //format elapsed time in seconds hh:mm:ss format long elapsed = elapsedtime() / 1000; long h = elapsed / 3600; long m = (elapsed % 3600) / 60; long s = elapsed % 60; if (h > 0) { homecoming string.format("%02d:%02d:%02d", h, m, s); } else { homecoming string.format("%02d:%02d", m, s); } } @override public void run() { while (running) { seek { handler.sendmessage(handler.obtainmessage()); thread.sleep(100); } grab (interruptedexception e) { e.printstacktrace(); } } } }
timerview
, custom view displays elapsed time
public class timerview extends view { private final paint mbg; private final paint mtext; private workoutplan plan; private timerthread timer; private thread thread; private handler handler = new timerhandler(); public timerview(context context, attributeset attrs) { super(context, attrs); plan = new workoutplan(); timer = new timerthread(plan, handler); thread = new thread(timer); mbg = new paint(); mbg.setcolor(getresources().getcolor(r.color.bg_default)); mtext = new paint(); mtext.setcolor(getresources().getcolor(r.color.text)); } public void start() { timer.start(); thread.start(); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); //draw current time , current round canvas.drawtext(timer.currtimestr(), 0, 50, mtext); } private class timerhandler extends handler { @override public void handlemessage(message msg) { super.handlemessage(msg); invalidate(); } } }
finally, workouttimer
, activity starts off
public class workouttimer extends activity { private timerview mtimer; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.timer); mtimer = (timerview) findviewbyid(r.id.timer_view); } @override protected void onstart() { super.onstart(); mtimer.start(); } }
a couple of questions:
where's best place createworkoutplan
object? it's done in timerview
. should done in workouttimer
activity instead? if yes, how pass workoutplan
object timerview
? the app works fine first time, crashes when go home screen app sec time. what's causing crash? theading? currently i'm using handler
in new thread
. ok or timertask
better? where's best place start thread? am right set threading code in timerview
? feels should somewhere else. am right add together synchronized methods in timerthread
? i'm trying create app can practice. please allow me know if there's improvements in technique should make, or if i'm doing incorrectly.
if asking on style, bad style utilize "timer" word in variants implementing of not timer, absolutely different class, handler. utilize other name. code unreadable.
android
Comments
Post a Comment