- Home /
Timer that can count Hour difference between two DateTime for android
Hi i need help on a timer that can tell the difference of hours or minutes passed after pause, minimize or quit of the android app, i really need help something that is tested and working, im not looking for something that runs in the background, i am looking something that saves the time in playerprefs on quit or pause and resumes the time after opening the app again, i know its about calculating the difference from the old time it was saved and from the datetime.now time, i also tried a script i found but it is not working when combined on exit and minimize operations. the script works when just minimizing and resuming, but once i exit the app. the timex gives different value like it starts from zero again, it also works when i just quit the app and open but when i mix quit app and minimize it does not go well...
import UnityEngine; import System.Collections; import System.DateTime; import System;
static var currentDate : DateTime; static var oldDate : DateTime; static var timex : double; static var temp : long; static var difference : TimeSpan;
var guiStyle : GUIStyle;
function Start() {
Screen.SetResolution(1280, 720, true);
Application.targetFrameRate = 29;
// THIS IS THE ORIGINAL
//Store the current time when it starts
currentDate = System.DateTime.Now;
//Grab the old time from the playerprefs as a long
temp = Convert.ToInt64(PlayerPrefs.GetString("timeData"));
//Convert the old time from binary to a DataTime variable
oldDate = System.DateTime.FromBinary(temp);
// print("oldDate: " + oldDate);
//Use the Subtract method and store the result as a timespan variable
var difference : TimeSpan = currentDate.Subtract(oldDate);
// print("Difference: " + difference.TotalSeconds);
timex = timex + difference.TotalSeconds;
}
}
//////////////////////////////// ///// ON PASUE /////////////// function OnApplicationPause(status : boolean) { ///////////////////////////////////////////////
/////// ON MINIMIZE /////////
///////////////////////////// if(status == true) { //Save the current system time as a string in the playerprefs class PlayerPrefs.SetString("timeData", System.DateTime.Now.ToBinary().ToString()); // PlayerPrefs.Save();
Debug.Log("App Minimized");
}
/////// ON APP RESUME /////////
/////////////////////////////// if(status == false) {
//Store the current time when it starts
currentDate = DateTime.Now;
//Grab the old time from the playerprefs as a long
temp = Convert.ToInt64(PlayerPrefs.GetString("timeData"));
//Convert the old time from binary to a DataTime variable
oldDate = System.DateTime.FromBinary(temp);
// print("oldDate: " + oldDate);
//Use the Subtract method and store the result as a timespan variable
difference = currentDate.Subtract(oldDate);
// print("Difference: " + difference.TotalSeconds);
timex = timex + difference.TotalSeconds;
Debug.Log("App Resumed");
}
} ///////////////////////////////////////////////
function OnApplicationQuit() { //Save the current system time as a string in the playerprefs class PlayerPrefs.SetString("timeData", System.DateTime.Now.ToBinary().ToString()); // PlayerPrefs.Save();
Debug.Log("Quited");
}
function OnGUI() {
////// GUI Styles
guiStyle.fontSize = 38;
guiStyle.normal.textColor = Color.white;
////// DISPLAY TIME B
GUI.Label(Rect(20,20,300,40), "Time: " + timex, guiStyle);
}
The below code works with time saving and loading properly BUT only when the app was closed or removed from recent apps in device, but does not work when user press HO$$anonymous$$E or $$anonymous$$imize the app, i tried to use OnApplicationPause then called Application.Quit(); to simulate like if you leave the app close the app so timers work properly and not freeze like nothing happened, now that problem is OnApplicationPause does not work in my device.. i dunno why, for debugs it work but when i use Application.Quit() on OnApplicationPause does not work.. any help please... anyone..
function Start() {
//Store the current time when it starts
currentDate = System.DateTime.Now;
//Grab the old time from the playerprefs as a long
var temp : long = Convert.ToInt64(PlayerPrefs.GetString("timeData"));
//Convert the old time from binary to a DataTime variable
oldDate = System.DateTime.FromBinary(temp);
//Use the Subtract method and store the result as a timespan variable
var difference : TimeSpan = currentDate.Subtract(oldDate);
timex = timex + difference.TotalSeconds;
}
Answer by andyblueAMS001 · Aug 30, 2020 at 09:18 AM
what i want to do is if the user press home button it will force the app to quit so the timers work properly but i am already getting tired of trying alot of variation but it really seems that it does not accept the Pause state... i cannot force the player to always close the app from recents list everytime he does not want to play the app anymore... that why what i have in mind is to trick the Home button by triggering a quit even by using a Home or minimize, i know Home button is link to OnApplicationPause but what can you do if it does not WORK??