java - How do I save chronometer elapsed time in android app -


i'm developing sort of parking meter counter app few specific locations. app asks user (dropdown menu, saves column letter , number, a6) , shows screen given location, timer (chronometer class) counting 0 , price has paid on exit (calculated using elapsed hours * base price).

this works nicely, until user or os kills app (task manager or memory management). then, next time user opens app, he's @ main menu , location, time , price has been lost.

i need way save of user's information , able load on app restart.

initially had thought save user's location , exact time chronometer started (datetime.now() maybe?) .txt file in internal storage read:

 mallname,columnletter,columnnumber,starttime 

this way, if app killed, or time app started scratch, mainactivity first check if file.txt exists in app's internal storage, if does, starts lastpage activity, reading .txt file pass values parameters (thus, comma separation). if there's no such file, carry on normally. last page contain reset button delete file app can start next time it's used.

i don't know how efficient method is, it's first thing came mind. however, don't know how go this. have same exact method programmed in c# windows phone, don't know how translate java android. also, in c# didn't use chronometer, rather, had starttime saved , used timer calculate starttime - datetime.now() on every tick (every second) , update textblock show this.

any appreciated.

usually user data should saved sqlite database, in case, since you're talking few variables, it'd easier save these values in application's sharedpreferences.

for every value you'd save, add line chronometer's onchronometertick() method:

preferencemanager.getdefaultsharedpreferences(this).edit().putint("your_key", <your_value>).commit(); 

this can later (whenever application recovers crash, example) retrieved by:

preferencemanager.getdefaultsharedpreferences(this).getint("your_key", <default_value>); 

regarding performance issues, if chronometer ticks once every second should have no effect on app's performances , acceptable.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -