Objective
As Android developers, presumably more or less to touch the start speed optimization related things, when the user more and more, the product function with more and more iterations, the App gradually become bloated is a very common phenomenon, even can be said to be unavoidable phenomenon, followed by the work is to optimize the performance of the app, One of the most important is the start-up speed optimization. but the main character of this article is not start speed optimization, but start time statistics.
First, Startup type
工欲善其事, its prerequisite. To optimize the start-up speed of the APP, you must have an accurate measure of the start time, otherwise the effect after optimization, I do not know, say to others are not convinced. Before doing APP startup time statistics, of course you have to figure out what startup types are, and what each startup type features. In general, the startup methods used in Android are grouped into the following categories:
1, Cold start: When the application is started, the background does not have the application process, then the system will re-create a new process assigned to the application, this startup mode is cold start. Cold start because the system will re-create a new process to assign it, the class is created and initialized first, Application
then the class is created and initialized MainActivity
, and finally the interface is displayed.
2, hot start: When the application, the background has the application of the process (example: Press the back key, the home button, although the application will exit, but the application of the process is still remain in the background, can enter the task list to view), so in the case of the existing process, This startup will start the application from the existing process, which is called hot start. Hot start is starting from an existing process, so hot start will not go Application
this step, but go directly MainActivity
, so the hot start process does not have to be created and initialized Application
, because one application from the creation of the new process to the destruction of the process, Application
will only initialize once.
3, the first start-up: the first start-up is strictly cold start, the reason for the first start to separate listed, in general, the first start time will be longer than the first start, the first start will do some system initialization work, such as cache directory production, database establishment, Sharedpreference initialization, if there are many Dex and Plug-ins in the case, the first boot will have some special need to deal with the logic, but also have a great impact on the start speed, so the first start speed is very important, after all, affect the user's first image of the App.
Second, the local start time statistical method
If it is local debugging, the statistics start time is very simple, through the command line method:
ADB shell am start-w packagename/activity
The results of the output are similar to the following:
$ adb shell am start-w com.speed.test/com.speed.test.homeactivity
starting:intent {act= Android.intent.action.MAIN Cat=[android.intent.category.launcher] cmp=com.speed.test/. homeactivity}
Status:ok
activity:com.speed.test/. Homeactivity
thistime:496
totaltime:496
waittime:503
Complete
WaitTime
Returns the startActivity
full display of the time from the first frame to the application. Is the total time consuming, including the previous application and the time of Activity pause
new application start-up;
ThisTime
Indicates that the Activity
last boot time of a series of launches is Activity
time-consuming;
TotalTime
Represents the time consuming to start a new application, including the start and Activity
start of a new process, but excluding Activity
the time consuming of the previous application pause.
Developers generally as long as the concern TotalTime, this time is the application of their own real start-up of time-consuming.
Third, the online start time statistical method
When the app is posted online, you want to count the startup speed of the app on the user's phone, and it's not going to be counted by the command line, which is basically sending the boot time up by playing Log. Then where to add the start time statistics log is particularly important, log added location directly determines whether the start time statistics are accurate, the same will affect the start speed optimization effect of judgment. To find the correct log of the startup time, you need to know the startup process of the application and the order in which each lifecycle function is called. The following to analyze the location in the end of the log record start time is more appropriate.
The main start-up process of the application
There are a lot of articles about the APP startup process, and there are some reference articles at the bottom of the article, which only outline the following:
1, by Launcher
starting the application, click on the application icon, Launcher
call to startActivity
start the application.
2, Launcher Activity
the final Instrumentation
call execStartActivity
to start the application.
3, Instrumentation
ActivityManagerProxy
the call (Activitymanagerservice in the application process of a proxy object) object to startActivity
start the method Activity
.
4. So far all processes are executing within the Launcher
process, and then ActivityManagerProxy
the object is invoked across ActivityManagerService
the process (running in the system_server process) to startActivity
start the application.
5, ActivityManagerService
the startActivity
method after a series of calls, the final call zygoteSendArgsAndGetResult
by socket
sending zygote
to the process, the zygote
process will hatch a new application process.
6. After the zygote process has hatched a new application process, the ActivityThread
class method will be executed main
. In this method, the message queue is prepared first Looper
and the method is then invoked to attach
bind the application process to ActivityManagerService
, then go into the loop
loop, read the message queue continuously, and distribute the message.
7 ActivityManagerService
. Save a proxy object for the application process and then ActivityManagerService
notify the application process through the proxy object to create an instance of the portal Activity
and execute its lifecycle function.
The summary process is: The user Launcher
clicks the application icon in the program, will notify the ActivityManagerService
launch of the application of the portal Activity
, found that ActivityManagerService
the application has not yet started, will inform the Zygote
process of hatching the application process, and then in this application process to execute ActivityThread
the main
method. The application process then notifies the application that the ActivityManagerService
process has started, ActivityManagerService
saving a proxy object for the application process, so that the application process ActivityManagerService
can be controlled through this proxy object, and then the ActivityManagerService
application process is notified to create an instance of the entry activity and execute its lifecycle function.
Life cycle function Execution process
The startup process above is a mechanism provided by Android, and as a developer we need to know or at least understand the processes and principles, but we are not able to do anything in this process, and what we can do is just start with the last step in the process, which is to ActivityManagerService
notify the application process creation portal through the proxy object Activity
and execute its lifecycle function to begin with, our Start time statistics and startup speed optimization are also starting from here. The following is the startup process for Main activity:
-> Application Constructor
-> application.attachbasecontext ()-> application.oncreate
()->
Activity constructor
-> activity.settheme ()
-> activity.oncreate ()
->
Activity.onstart Activity.onresume
-> activity.onattachedtowindow
-> activity.onwindowfocuschanged
If you log the startup time of the APP, record at least two points, a starting point, an end point.
Starting point in time
The starting point is easier to record: if the record cold start start time can generally Application.attachBaseContext()
record the starting point at the beginning of the location, because it has Context
not been initialized before, generally do not do anything, of course, this is to depend on the specific circumstances to decide, in fact, as long as the guarantee in the APP The specific business logic begins to record the starting point of time before it is executed. If you record a hot boot start point, you can Activity.onRestart()
record the start point in.
End Time Point
The end point is theoretically chosen when app shows the first screen interface, but where does the app display the first screen? Many articles on the web say that Activity
after the completion of the onResume
method, it is visible to the Activity
user, in fact, is not, a Activity
walk through onCreate onStart onResume
these several life cycle, just completed the application of their own configuration, such as the Activity
Theme Set window property settings The establishment of the c10/> tree, but in fact, the following needs the View
implementation measure layout draw
. So the log at the end point in the log OnResume
is not accurate, you can pay attention to the last function in the process Activity.onWindowFocusChanged
, the following is its comment:
/** *called when the current
{@link Windows} of the activity gains or loses
* focus. This is the best indicator of whether the ' visible ' to the
user. The default implementation clears the key tracking
* State, so should always is called.
...
*/
We can see from the annotation that this function is the activity
best place to judge whether it is visible, so we can Activity.onWindowFocusChanged
record the end point of the application start, but we need to be aware that the function is Activity
triggered when the focus changes, so be sure to get rid of unwanted situations.
Summarize
The above is about Android (Android) development of the statistics app start time, the content of this article is still very important, or that sentence: 工欲善其事, its prerequisite, ready to do a full, natural and reasoned. I hope the content of this article is helpful to everyone.