1

I would like to track how much time take my app to start, but I would like to track this specific info defined here: https://developer.android.com/topic/performance/vitals/launch-time In order to have better graphics about it and add custom info to it. What I have tried?

I have tried to add a timer between onCreate method and onResume but the time obtained is the half of the time tracked by the system "Activity Manager: Displayed"

What can I do to have those values programmatically?

enter image description here

Pranav P
  • 1,755
  • 19
  • 39
Gilberto Ibarra
  • 2,849
  • 2
  • 27
  • 38

1 Answers1

0

You should count the time between Application.onCreate() to end of Activity.onCreate() to get the cold startup time. Please read here for more info on this.

Let me explain. In your application class:

class YourApplication: Application() {

public static long starttime = 0;

@Override
    public void onCreate() {
    starttime = SystemClock.elapsedRealtime();
}
}

In your launcher activity class, upon completion of onCreate method:

class MainActivity : Activity() {

       @Override
       public void onCreate() {
       long timeelapsed = (SystemClock.elapsedRealtime() - YourApplication.starttime);
     Log.i("TEST", "your time : "+ timeelapsed)
    }

}
Shalu T D
  • 3,921
  • 2
  • 26
  • 37
  • should I "stop" the timer at the begining of "onCreate" method ? , I am testing it to see what I get. – Gilberto Ibarra May 28 '20 at 05:39
  • This dont worked as the same way , i get 1500 ms vs 2s659ms . I tried to measure until "onResume" with the next result 1669 ms – Gilberto Ibarra May 28 '20 at 06:13
  • Isn't it better to check the elapsed time in Acitivity.onResume() instead? That's when the UI is rendered and active. – npace May 28 '20 at 06:31
  • that is right, but, the diference is at least 1 second! ... I know the doc says how it is measured, but, the result obtained is diferent – Gilberto Ibarra May 28 '20 at 06:37