android - Best Practice: How to draw a Countdown within a Service -
i´m new android programming , have question.
i have countdown count down 60 0 when touching screen. want countdown survive activity lifecycling when ondestroy called. need service that.
wenn countdown finished notification send. if user want open app again before countdown in service finished, no notification should sended , service has stopped.
what best practice implement features? here way go?
thank answers!
my approach have class countdown methods , service
//countdownclass timerclass.java
public class timerclass { private static final string tag = "mylog"; private static final long timerinterval = 1000; //eigenschaften -------------------------------------------------------------------- private boolean isactiv = false; private paint paint; private long timertime; private double actualtime; private int x; private int y; private countdowntimer countdowntimer; private string timestring; //konstruktor -------------------------------------------------------------------- public timerclass(int x, int y) { this.x = x; this.y = y; paint = new paint(); paint.setstyle(paint.style.fill); paint.setcolor(color.yellow); paint.setantialias(true); paint.settextalign(align.right); paint.settextsize(70); } public timerclass() { } //methoden -------------------------------------------------------------------- public void draw(canvas canvas) { if(isactiv) { canvas.drawtext(timestring, x, y, paint); } } public void createtimer(long timertime) { this.timertime = timertime; countdowntimer = new countdowntimer(timertime, timerinterval) { @override public void ontick(long millisuntilfinished) { actualtime = (double) millisuntilfinished / timerinterval; numberformat nf = new decimalformat("##.#"); nf.format(actualtime); if(actualtime < 10) { new decimalformat(); timestring = "0:0" + nf.format(actualtime - 1); } else { timestring = "0:" + nf.format(actualtime - 1); } } @override public void onfinish() { isactiv = false; } }; } public void timerstart() { countdowntimer.start(); isactiv = true; } public void timerstop() { if(isactiv) { countdowntimer.cancel(); } isactiv = false; } public boolean isactiv() { return isactiv; } public void setactiv(boolean isactiv) { this.isactiv = isactiv; } public double getactualtime() { return actualtime; } public void setactualtime(double actualtime) { this.actualtime = actualtime; } and service:
public class timerservice extends service { //class countdowntimer private timerclass timerclass; private long starttime; @override public ibinder onbind(intent intent) { return null; } @override public void oncreate() { super.oncreate(); timerclass = new timerclass(); starttime = (long) timerclass.getactualtime(); timerclass.createtimer(starttime); } @override public void ondestroy() { timerclass.timerstop(); super.ondestroy(); } @override public int onstartcommand(intent intent, int flags, int startid) { timerclass.timerstart(); if(timerclass.getactualtime() <= 0) { // send notification } return super.onstartcommand(intent, flags, startid); } i wanted start service in mainactivies onpause method , stop when onresume called. i´ve have tried many times , failed.. have no code service in activity.
( sorry broken english :( )
Comments
Post a Comment