How can I track the ActivityManager loading time of my app?

327 Views Asked by At

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

1

There are 1 best solutions below

4
On

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)
    }

}