0

I have some problems with the order of method in my app. How i can take the control of time in an Android app? How i can know how many time is elapsed from the start of app? For more accuracy, I would say at 3 seconds from the start of this app, i want make happen this, like an invisible chronometer that, at one second, applies a method that i choose. I hope the question is easy to understand. Thanks in advance!

LolloAAA
  • 112
  • 10

2 Answers2

2

If You want to run task at once only you can use

new Timer().schedule(task, after);

And for multiple time execution

new Timer().scheduleAtFixedRate(task, after, interval);

for more details java.util.Timer.

Keyur Lakhani
  • 4,321
  • 1
  • 24
  • 35
0

Take a look of this library, Hugo https://github.com/JakeWharton/hugo

With this you can control how many time elapsed in the methods.

To control how many time elapsed from app start until, for example, first activity is shown you have to:

First, extends Application class, you have and example here http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/

In the onCreate method of that class, save the time with System.currentTimeMillis() method

Then create a static method

public long getCurrentTimeFromStart() {  
    long current = System.currentTimeMillis();
    return mInitialTime - current;
}

Call this method in onCreate method of your MainActivity or only the first time is called onResume for a more accurate time.

And for the last question you can use @Keyur Lakhani answer and user Timer class to schelude Runnables

Aracem
  • 7,227
  • 4
  • 35
  • 72