I have been trying to setting up a function, which is getting the time since the window is appearing for the viewer.
I have been using a handler, timer, currenttimemillis.
That i have succeeded in doing. The window is showing the ongoing time since the window has been shown for the user – update frequency of 40fps, and i love how it looks and works..
BUT!
When going backwards and forwards into the window, the time restarts.
I want the window to show the (running) time since the initial view for the user and i don’t want the time “broken” even if the program is stopped.
I am using sharedpreferences for saving strings and i have tried to saving the initial System.currentTimeMillis(); as a sharedpreference (as a long) and loading it again on oncreate with this if statement
SharedPreferences sData14;
private final int REFRESH_RATE = 25;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.orderwaiting);
setupVariables();
sData14 = getSharedPreferences(filename, 0);
}
private void setupVariables() {
mHandler.removeCallbacks(startTimer);
mHandler.postDelayed(startTimer, 0);
if (sData14 == null) {
starttime = System.currentTimeMillis();
sData14 = getSharedPreferences(filename, 0);
SharedPreferences.Editor editor = sData14.edit();
editor.putLong("sharedString14", starttime);
editor.commit();
};
if (sData14 != null) {
Long value_long = sData14.getLong("sharedString14", 1000000);
starttime = value_long;
};
EDITED ADDED
I don’t use onresume and onpause.
Do i have to do that ?
I would expect the if statement handles the “case”.. and i need a function that handles the case in which the program has been “closed”, but the function is still needed to track how far we have reached in time.
Is it better to doinbackground, and be able to call it from another place as well ?
It just doesn’t load the shared data…
In other words, i need a dynamic number to use from when the user first is viewing this window. Maybe there are other ways i could do this as well ??
I guess it should be working “this way”.. but i can’t seem to make it work..
I hope i have set up my problem in an simple way.
SOLUTION
long startTime;
private final int REFRESH_RATE = 25;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.orderwaiting);
setupVariables();
}
private void setupVariables() {
SharedPreferences sData14 = getSharedPreferences(filename, 0);
startTime = sData14.getLong("sharedString14", -1);
**starttime = startTime;**
if(startTime == -1){
//startTime not previously saved
starttime = System.currentTimeMillis();
SharedPreferences.Editor editor = sData14.edit();
editor.putLong("sharedString14", starttime);
editor.commit();
}
mHandler.removeCallbacks(startTimer);
mHandler.postDelayed(startTimer, 0);
i added the starttime = startTime, since it would else not load any starttime on the second view..
Thanks to David.. You saved my day 🙂
Ah Ok, I missunderstood your initial question. The way you are checking your prefenences isn’t quite right, you can also simplify it quite a bit. The code below should fix your problems: