The startup time of an application is also an important indicator to measure the performance of the application, so let's take a look at how to get the time required for a program to start.
There are several methods to get the start time of a program. For details, see the following analysis:
Method 1: Get it through log.
Idea: record the start time and end time and calculate the time difference to get the start time.
Add log. e ("TAG", "" finish) to the last line of the onCreate () method, and view the log output in logcat to calculate the program startup time.
This method requires source code and is relatively primitive and is not recommended.
Method 2: Get the start time through ActivityManager training.
Train of Thought: Get the start time by calculating the time difference between the time when the rotation training does not appear and the time when it appears.
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ActivityManager am = (ActivityManager) getSystemService(this.ACTIVITY_SERVICE);List
appinfo = am.getRunningAppProcesses();for (RunningAppProcessInfo runningAppProcessInfo : appinfo) {if (runningAppProcessInfo.processName.equals("yourpakagename")) {Log.e("TAG", System.currentTimeMillis() + "");}}}
This method is more accurate than the first method without the source code.
Method 3: Call the application (shell am) to obtain the startup time.
Idea: Call the Android shell command to get the app startup time.
adb shell am start -W -n yourpakagename/MainActivity
The start time obtained by this method is very accurate and accurate to milliseconds.
To sum up the three methods, method 1 is relatively primitive but easy to operate. It is suitable for beginners to use black box tests with low requirements. method 2 has high technical requirements, this method is suitable for apps developed on mobile phones to detect the startup time of other apps. Method 3 is suitable for batch viewing the application startup time. The disadvantage is that LanuchActivity of all applications must be obtained.