Timing errors in our game, currentTimeMillis freezing?

When testing our game, which is highly dependent on System.currentTimeMillis() , we experience an annoying error.

Our game uses an array of delta tags, which indicates when certain things happen. These timestamps correspond to the part of the music being played.

Testing at home does not give us any problems. Unable to reproduce error while testing from our home.

But testing while driving in a car between cities gives us problems of synchronization between time stamps and music. My best guess is that Android will freeze the system, including the system timer, because it is switching the network or looking for a signal?

I tried to embed a fake tooltip into the game by creating a thread for a few seconds when I press a specific button. This freezes the screen (obviously), but it still syncs perfectly when the dream is over.

The only way to reproduce this mistake is to make a trip by car or bus or train, which, of course, is most likely when most people will play in our game.

Question of course

  • what to do with it?

  • Does anyone have any ideas?

+4
source share
1 answer

Read SystemClock .

System.currentTimeMillis () is a standard "wall" clock (time and date) that expresses milliseconds from an era. A wall clock can be user-set or a telephone network (see setCurrentTimeMillis (long)), so time can jump back or forward.


uptimeMillis () is counted in milliseconds since the system boots. This clock stops when the system goes into deep sleep (the CPU is off, a dark one is displayed, a device is waiting for an external input), but it is not affected by the hours of scaling, downtime or other energy-saving mechanisms. This is the basis for most time intervals, such as Thread.sleep (millls), Object.wait (millis) and System.nanoTime () . This watch is guaranteed to be monotonous and suitable for interval synchronization when the interval does not cover sleep time.

I think it is better to use System.nanoTime () .

+3
source

Source: https://habr.com/ru/post/1484053/


All Articles