Android startup optimization instructions, black and white screen processing

foreword

  

text

  In the process of using Android App, the optimization of the application is a bonus item. For example, it takes 2 seconds to open your App, and 0.5 seconds for others. This is a great optimization of user experience. Of course, I haven't seen an app that starts up so fast. What's the reason? Because the app does too many things when it starts. This is for those commercial apps. To use, first let's think about startup optimization.

1. Startup history

  This startup optimization was not used on apps at first, but on web pages. In the 1900s, the computers at that time were still big and bulky.
insert image description here
Just like this kind of computer, I was in an Internet cafe when I was a kid... No, I saw it in the computer room of the school. There was one class a week. The computer didn't have anything, and I didn't know how to type. There is an 8-second rule when opening a web page, which means that if your web page is opened for more than 8 seconds, the user churn will reach 70%. Therefore, optimization is necessary. This is for web pages, then for App For how long?

  Now is the era of mobile Internet, many people can not use a computer, but cannot use a mobile phone. Then, for the apps installed on the mobile phone, the regular application opening time is divided into several grades, the time: 2s, 2 ~ 5s, 5 ~ 8s, 8s+.

Below I use a few pictures to show how the four time levels feel to users:

First is 2s
insert image description here
2 ~ 5s
insert image description here
5 ~ 8s
insert image description here
8s+
insert image description here

So it is necessary to start optimization.

2. Startup Instructions

  There are two types of startup for installation, one is the startup of the Android system, and the other is the startup of the App application.

  The startup of the Android system is the startup of the mobile phone. The whole process is from the startup of the mobile phone to the desktop of the mobile phone. The process inside is as follows:

  1. power up (boot)
  2. BootLoader (system boot chip wake-up)
  3. Linux Kernel (init.rc)
  4. Init process (ID is 1)
  5. Zygote process
  6. ART, SystemServer and other system services
  7. Binder (thread pool) SSM (system message), AMS (startup App), PMS (package management) and other system services
  8. Launcher (mobile phone system desktop)

  The following is the startup of the App, which is also mainly explained in the article. The startup of the app is divided into three parts:

  1. Cold start
  2. Hot Start
  3. warm start

Maybe you are relatively unfamiliar with these terms, here is a brief introduction:

1. Cold start

  For example, you click the icon of the app on the phone desktop for the first time after you turn it on to open the app, or you open the app again after your app process has been killed for a period of time. This is a relatively simple statement. A little more concise is the first startup, there is no application process in the background .

2. Warm boot

  The user exits the current application but the process is not killed (destroyed). For example, I am using a certain App, and then I receive a WeChat message, I click the message to go to WeChat, and then the App enters the background from the foreground. But the app is still running in the background and the process is not killed. At this point, when you enter the app again, it is called a hot start.

3. Warm start

  A warm start is somewhere between a cold start and a hot start. Let me say an inappropriate example. In ancient China, the execution ground was beheaded. According to the method of the TV series, when the knife and axe hand raised the knife to kill before killing, a sentence was executed. If you keep someone, then the person will come back to life. Switching to the application means that after I end the process of the application, I immediately open the application again. At this time, its process will be destroyed in memory, but it takes time to destroy, not to say that it is destroyed. At this point, if you start the app again, then its operation links are less than cold start, and more hot start is more, and the compromise operation is warm start. Just like when you add cold water to hot water, the water will become warmer.

The three startups are briefly explained. In fact, both hot startup and warm startup will go through part of the process of cold startup, so we can optimize the cold startup.

4. Cold Start Process

  1. The user clicks the App icon on the desktop, and the Launcher process uses Binder IPC (Inter-Process Communication, inter-process communication) method to send a startActivity request to system_server.
  2. After the system_server process receives the request, it sends a request to create an App process to the Zygote process.
  3. The Zygote process forks out a new child process, the App process.
  4. The App process sends an attachApplication request to system_server through Binder IPC.
  5. After the system_server receives the relevant request, it makes a series of preparations, and sends the scheduleLaunchActivity request to the App process through the Binder IPC.
  6. After receiving the request, the binder thread (ie ActivityThread) of the App process sends a LAUNCH_ACTIVITY message to the main thread through the handler.
  7. After receiving the message, the main thread creates the related Activity through reflection, and calls back methods such as Activity's onCreate.
  8. At this point, the App is officially launched, and it begins to enter the Activity life cycle. After executing onCreate/onStart/onResume, after rendering the UI interface, you can see the main interface of the App.

5. Optimization time

  From the above points, we know the type, content, and process of startup optimization, so what are we optimizing? It is time. Four levels of time are mentioned at the beginning of the article. Optimization time is to shorten the cold start time of your application. It is also popular, how long it takes to enter the main page of the app from the time you click the desktop icon. The sooner this time, the better.

  So first of all we should get the startup time, how to get it? By CPU Profile , where is this thing?
insert image description here
Click Edit Configurations… and select Profiling in the pop-up window.
insert image description here
Set it up as shown above and click OK. Here I created a new project and modified the code in MainActivity.
insert image description here
Then start the virtualizer. If you want to know how much time the project's method execution takes at runtime, you can click the dashboard-like icon in the image below.
insert image description here
After clicking, your app will also run, but the usage time of the method will be collected during the running process. As shown in the figure below:
insert image description here
At this time, the waveform on the right side, you can observe whether it is stable, and you can stop after it is stable.
insert image description here
The method of switching Top Down after stopping
insert image description here
is called in onCreate, so let's search and keep looking.
insert image description here
Found it, here we find that the abc() method takes 300,054 microseconds, which is 300 milliseconds, here we can see the time consuming of this method, and if we know which method takes time, we can optimize the method. Similarly, you will see To onCreate, setContentView() will also be executed, here is to load xml to render the page, and then onCreate() of the parent class.

6. Optimization plan

  For the development and use of the App, there are the following optimization schemes:

  1. To reduce the nesting of xml layout and avoid over-drawing, we have to mention constraint layout (PS: I also use less of this myself)
  2. As little as possible to write initialization code in the onCreate() method, you can initialize it in onWindowFocusChanged().
  3. Lazy loading, lazy loading, in general, is to avoid performing time-consuming operations in the main thread, such as accessing the network, data reading and writing, database operations, etc.
  4. To improve code quality, you can refer to Ali Coding Specification, download address: Ali Coding Specification-Android
  5. Black and white screen, enhance the visual effect, and then feel very fast.

Three, black and white screen processing

  When the default Android App starts, there will be a preview page. This preview page is black in the low version of Android and white in the high version, commonly known as black and white screen, for example,
insert image description here

You will see a brief blank screen. Then let's take a look at the launch of NetEase Cloud Music.

insert image description here
It can be seen that there is no white screen. In fact, the preview page of this white screen is used. In fact, we can also imitate the startup of NetEase Cloud. How to do it.

1. Create a splash page

  First, we create a startup page and create a new SplashActivity. The corresponding layout is activity_splash.xml. In this xml, we only put a TextView control.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SplashActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="网易云音乐"
        android:textColor="@color/white"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.96" />
</androidx.constraintlayout.widget.ConstraintLayout>

Then just make a white music icon, you can go to my source code to get it.

2. Style

  Because it is a startup page imitating this NetEase Cloud, a red color value is required, and the following code is added to colors.xml:

<color name="red">#CE281B</color>

Then add a theme style to themes.xml.

	<!--启动页主题样式-->
    <style name="SplashTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="colorPrimary">@color/red</item>
        <item name="colorPrimaryVariant">@color/red</item>
        <item name="statusBarBackground">@color/red</item>
        <item name="android:statusBarColor">@color/red</item>
        <item name="android:windowBackground">@drawable/splash_bg</item>
    </style>

Then create a splash_bg.xml under drawable with the following code:

<!--启动页主题样式-->
    <style name="SplashTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="colorPrimary">@color/red</item>
        <item name="colorPrimaryVariant">@color/red</item>
        <item name="statusBarBackground">@color/red</item>
        <item name="android:statusBarColor">@color/red</item>
        <item name="android:windowBackground">@drawable/splash_bg</item>
    </style>

Finally, set the theme in AndroidManifest.xml
insert image description here
Set SplashActivity, and then set the startup page.

Run it below:
insert image description here

See how this works, you will see that I have no animation effect, but the effect is displayed behind this text. Why, it still comes down to this preview page. This preview page comes out first, and then SplashActivity, so the text is displayed behind, and then we can delay 500 milliseconds before jumping to MainActivity.

3. Operation effect

insert image description here

Fourth, the source code

GitHub address: WhoCare
CSDN address: WhoCare.rar

Five, happy moment

There is no bottleneck in the emoji package of the National Health Commission
insert image description here

Guess you like

Origin blog.csdn.net/qq_38436214/article/details/123342433