Reputation: 989
I want to make a idlegame and for that I need to get the time in which the app was closed so I can calculate the income in that offline-time.
My first idea was to get the currentTimeMillis and save them via sharedPreferences and when opening the app again I calculate the difference between the current and the saved time. My problem is the sharedPreferences Variable seems to be 0 all the time.
my Code:
Zeit = System.currentTimeMillis() / 1000L;
Zeit_Differenz = Zeit - Zeit_SAVE;
Log.i("e","aktuelle Zeit: " + Zeit);
Log.i("e", "gespeicherte Zeit: " + Zeit_SAVE);
Log.i("e", "errechnete differenz: " + Zeit_Differenz);
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong("Zeit save", Zeit);
editor.apply();
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
Zeit_SAVE = sharedPreferences.getLong("Zeit save", 0);
Logcat:
aktuelle Zeit: 1569344292 (time right now)
gespeicherte Zeit: 0 (saved time)
errechnete differenz: 1569344292 (calculated difference)
There is other code between these snippets. I just copied the most important code for you.
I hope you can help me, this really is my only idea of how to accomplish this.
Upvotes: 0
Views: 265
Reputation: 76569
Just override methods Activity.onPause()
and Activity.onResume()
in order to save the timestamp and then later perform the calculation. That one whitespace in the preference's name Zeit save
might cause it to always return the default value 0
; better replace that with an _
underscore, eg. timestamp_paused
.
Upvotes: 1
Reputation: 338181
I cannot help with the persisting of values to storage, as I do not use Android. But I can show how to record current moment and later calculate elapsed time.
Record current moment.
Instant.now().toString()
"2019-09-24T20:50:52.827365Z"
Parse that string, and capture elapsed time:
Duration // Represent a span-of-time not attached to the timeline.
.between( // Calculate time elapsed between a pair of moments.
Instant.parse( "2019-09-24T20:50:52.827365Z" ) , // Parse string in standard ISO 8601 format. The `Z` on the end means UTC, pronounced “Zulu”.
Instant.now() // Capture the current moment in UTC.
) // Returns a `Duration` object.
.toMillis() // Interrogates the `Duration` object for its total elapsed time in milliseconds, effectively truncating any microseconds/nanoseconds.
The modern approach to tracking time uses the java.time classes, specifically:
Instant
to represent a moment in UTCDuration
to represent a span-of-time unattached to the timeline, basically a count of nanoseconds.Capture the current moment in UTC.
Instant instant = Instant.now() ;
Persist a textual representation of that value using standard ISO 8601 format.
String output = instant.toString() ;
Read that stored string, and parse as an Instant
.
String input = "2019-09-24T20:50:52.827365Z" ;
Instant then = Instant.parse( input ) ;
Capture the current moment, and calculate elapsed time as a `Duration.
Instant now = Instant.now() ;
Duration d = Duration.of( then , now ) ;
If you want the elapsed time as a total count of milliseconds, interrogate the Duration
object.
long milliseconds = d.toMillis() ; // Total elapsed time in milliseconds, truncating any microseconds/nanoseconds.
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
Upvotes: 1