Android studio get time from my timer, in order to get distance -
i have created timer, , works well. , wanna use timer, lets say, every 5 seconds, location of user. , test if method works or not. put oxy++
(field in mainactivity) in it, , see if runs in every 5secs. , got problem.
public class mapsactivity extends baseactivity /*implements locationlistener*/{ int oxy = 0; private int isreset = 1; private textview texttimer; private button startbutton; private button pausebutton; private button resetbutton; private long starttime = 0l; private handler myhandler = new handler(); long timeinmillies = 0l; long timeswap = 0l; long finaltime = 0l; private string[] navmenutitles; private typedarray navmenuicons; private googlemap mmap; private button testbtn; double x; double y; double j; double k; float dis; textview txt; location locationa; location locationb; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_maps); setupmapifneeded(); texttimer = (textview) findviewbyid(r.id.texttimer); startbutton = (button) findviewbyid(r.id.btnstart); startbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { starttime = systemclock.uptimemillis(); myhandler.postdelayed(updatetimermethod, 0); //locationmanager locationmanager = (locationmanager) getsystemservice(location_service); //location locationa = locationmanager.getlastknownlocation(locationmanager.gps_provider); //mmap.setmaptype(googlemap.map_type_normal); //x = locationa.getlatitude(); //y = locationa.getlongitude(); } }); pausebutton = (button) findviewbyid(r.id.btnpause); pausebutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { timeswap += timeinmillies; myhandler.removecallbacks(updatetimermethod); } }); resetbutton = (button) findviewbyid(r.id.btnreset); resetbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { texttimer.settext("0:00:00"); timeswap=0; } }); } @override protected void onresume() { super.onresume(); setupmapifneeded(); } private void setupmapifneeded() { if (mmap == null) { // try obtain map supportmapfragment. mmap = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map)) .getmap(); // check if successful in obtaining map. if (mmap != null) { setupmap(); } } } private void setupmap() { mmap.setmylocationenabled(true); } private runnable updatetimermethod = new runnable() { public void run() { timeinmillies = systemclock.uptimemillis() - starttime; finaltime = timeswap + timeinmillies; int seconds = (int) (finaltime / 1000); int minutes = seconds / 60; seconds = seconds % 60; int milliseconds = (int) (finaltime % 1000); texttimer.settext("" + minutes + ":" + string.format("%02d", seconds) + ":" + string.format("%03d", milliseconds)); myhandler.postdelayed(this, 0); int test = (int) (finaltime%5000); if (test==0) { oxy++; } txt.settext("o = "+ test+ " "+ oxy); } };
runnable updatetimermethod loop, runs every millisecond. if use seconds%5000, runs when every 5s, oxy keeps++ til milliseconds have been added increase seconds 6. use finaltime in millisecond, %5000. doesn't work in every 5s, oxy+1 irregularly in multiple of 5 when re-run program, lets oxy+1 in 25s, 55s, 85s in 1st time. re-run it, oxy+1 in 45s, 65s, 105s. can tell why happened? there way solve this? wanna use location in every 50s. in advance!
using method handler.postdelayed
not idea time sense stuff, since not accurate enough. rather use timeanimator (api11+, or nineoldandroids below).
basic usage of timeanimator:
timeanimator anim = new timeanimator(); anim.settimelistener(new timeanimator.timelistener() { long time = 0; @override public void ontimeupdate(timeanimator timeanimator, long t, long dt) { time += dt; if (time >= 5000) { // >= needed because might not totally accurate... time -= 5000; // keep remainder (if there is) correct accuracy of next loop // stuff here (in every 5 seconds) } } });
Comments
Post a Comment