In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the start-up process of android performance optimization example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
I. the start-up mode of the application
Generally speaking, there are two ways to start: cold start and hot start.
1. Cold start: when the application is started, there is no process for the application in the background, and the system will re-create a new process to assign to the application. This startup method is cold start.
2. Hot startup: when you start the application, there is already a process of the application in the background (for example, press back or home button, and the application will exit, but the process of the application will still remain in the background and can be viewed in the task list). Therefore, in the case of an existing process, this startup will start the application from the existing process, which is called hot startup.
Characteristics
1. Cold start: cold start because the system will re-create a new process assigned to it, so it will first create and initialize the Application class, then create and initialize the MainActivity class (including a series of measurements, layouts, drawings), and finally display it on the interface.
2. Hot start: since hot start will start from an existing process, it will not take the step of Application, but will go directly to MainActivity (including a series of measurements, layouts, drawings), so the process of hot start only needs to create and initialize a MainActivity instead of creating and initializing Application
Because an application is initialized only once from the creation of a new process to the destruction of the process.
Second, the startup process of the application
Cold startup process: when you click the startup icon of app, the Android system will create a new process from the Zygote process to assign to the application, and then create and initialize the Application class, create the MainActivity class, and load the
WindowBackground and other properties are set to MainActivity and configure some properties at the Activity level, then inflate layout, and when the onCreate/onStart/onResume method is finished, the measure/layout/draw of contentView is displayed on the interface, so until here
The first launch of the application is complete, and the interface we see is the first frame. So, to sum up, the startup process of the application is as follows:
The constructor method of Application-- > attachBaseContext ()-- > onCreate ()-- > construction method of Activity-- > onCreate ()-- > configure properties such as background in the topic-- > onStart ()-- > onResume ()-- > Survey layout drawing is displayed on the interface.
The general process is as follows:
1. Click the desktop icon, and Launcher will start the default Acticity of the program, and then start various Activity according to the logic of the program.
2. Starting Activity requires the help of the ActivityManagerService service process in the application framework layer (Service is also started by the ActivityManagerService process); in the Android application framework layer, ActivityManagerService is a very important interface
It is responsible not only for launching Activity and Service, but also for managing Activity and Service.
Step 1. Whether you start Activity through Launcher or start a new Activity by calling startActivity interface inside Activity, you enter the ActivityManagerService process through Binder inter-process communication and call the ActivityManagerService.startActivity interface
Step 2. ActivityManagerService calls ActivityStack.startActivityMayWait to prepare information about the Activity to start
Step 3. ActivityStack notifies ApplicationThread that it is time to schedule Activity startup. Here ApplicationThread represents the process that calls the ActivityManagerService.startActivity interface. For the scenario where the application icon is clicked, the process is Launcher.
For the scenario where startActivity is called within the Activity, this process is the process in which the Activity is located.
Step 4. ApplicationThread does not perform the real startup operation, but enters the ActivityManagerService process by calling the ActivityManagerService.activityPaused interface to see if a new process needs to be created to start Activity
Step 5. For scenarios where Activity is started by clicking on the application icon, ActivityManagerService calls startProcessLocked to create a new process in this step, while this step is not required for starting a new Activity by calling startActivity within Activity
Because the new Activity is started in the same process as the original Activity
Step 6. ActivityManagerServic calls the ApplicationThread.scheduleLaunchActivity interface to notify the corresponding process to start Activity
Step 7. ApplicationThread forwards the operation of starting Activity to ActivityThread,ActivityThread to import the corresponding Activity class through ClassLoader, and then starts it.
Third, the white screen and black screen encountered in the cold start-up process and the optimization of start-up time
1. White screen problem:
In order to enable us to deploy code quickly, there is actually a set of very complex logic behind android studio upgrade 2.0. for example, to set up a server in APK to communicate with Android Studio, and to compare and replace code differences, white screen problems may occur in the process of research and development.
The general release version of the program will not have this phenomenon.
If there is a white screen problem next, you can view the style file
.truetrue
Add two properties, windowIsTranslucent and windowNoTitle, and set these two properties to true, so that the window of the program is transparent during initialization, and the main interface of the program will not be displayed until after initialization, so that the white screen interface can not be seen at all.
2. Optimization of startup time
First measure the startup time of activity-reportFullyDrawn () method of Activity
You need to call reportFullyDrawn () of Activity. It will report in log how long it took from apk initialization (the same time as the previous Displayed) to the reportFullyDrawn () method being called.
The log displayed by the reportFullyDrawn () method is similar to this:
ActivityManager: Displayed com.Android.myexample/.StartupTiming: + 768ms
Calling the reportFullyDrawn () method on 4. 4 will crash (but log can still print normally), indicating that UPDATE_DEVICE_STATS permission is required, but only the system app can grant this permission. The solution is to adjust it like this.
Try {reportFullyDrawn ();} catch (SecurityException e) {}
Another way to measure startup time is also worth mentioning, which is the screenrecord command.
First start the screenrecord command with the-bugreport option (which can add a timestamp to frames-it should be a feature in L):
$adb shell screenrecord-- bugreport / sdcard/launch.mp4
Then click the app icon, wait for app to display, ctrl-C screenrecord, and use the adb pull command to export the file to your computer.
$adb pull / sdcard/launch.mp4
Now you can open the recorded video and see what happens. You need a video player that can be viewed frame by frame (Quicktime on mac is fine, but it's not clear what player is best available on other os). Now play it frame by frame and notice that there is a frame timestamp at the top of the video.
Keep going until you find that the app icon is highlighted. By this time, the system has handled the click event on the icon, started app, and recorded the time of this frame. Continue to play the frame until you see the first frame of the entire UI of app. Depending on the situation (whether there is a startup window, whether there is a startup screen, etc.)
The actual order in which events and windows occur may be different. For a simple app, you will first see the launch window, and then fade into the real UI of app. After you see anything on the UI, you should record the first frame, when the app has finished laying out and drawing, and is ready to display it. At the same time, record the time of this frame.
Now subtract the two times ((UI displayed)-(icon tapped)); get the time from the click of the app to the time the drawing is ready. Although this time includes the time before the process starts, at least it can be used to compare with other app.
Optimization of cold start time of Android
Cold start time is the time between the moment the user clicks on your app and the time the system calls Activity.onCreate (). During this period of time, WindowManager will first load the windowBackground in the app theme style as the preview element of app, and then actually load the layout layout of activity
Cold start time optimization
Once you know how Android cold start time works, you can optimize the cold start time with a few tricks to make your app load "faster" (visually fast). We can make a .9 picture of the background style that starts Activity, and then use this .9 picture as a windowBackground.
After the image is made, we can use it as a preview element for the cold start phase of app, as follows:
Customize a Theme for the launched Activity
@ drawable/window_background_statusbar_toolbar_tab
Apply the new Theme to the settings to the AndroidManifest.xml
Since a new Theme has been set for MainActivity, this will overwrite the original Theme, so you need to set it back to the original Theme in MainActivity
Public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {/ / Make sure this line comes before calling super.onCreate (). SetTheme (R.style.AppTheme); super.onCreate (savedInstanceState);}} these are all the contents of the article "sample Analysis of the Startup process for android performance Optimization". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.