Wang Gang performance optimization ---- (the difference between black and white screen to solve the problem, use trace tools, hot start and cold start) APP startup optimization

One: the phone starts
Here Insert Picture Description
this time will start HomeActivity; and a variety of APP icon is HomeActivity of controls that are clickable. Corresponding is, Launcher (inherited Activity) has onClick (View view) method. We click on the onClick method and they will put information on this application is passed to a Intent passed inside. Object tag=v.getTag(); final Intent intent =((ShortCutInfo)tag).intent;Click to which application, which application is v, v.getTag (); this tag is to get the application ..
Then call startActivity (intent);. Start the corresponding app, here's intent is a process information. This time will inform our system, I want to open this process. The AMS system will put into the process virtual machine's memory, and open up the heap, stack, constant pool, method memory area and so on. Remember not, there is a background incubator (zygote) process, zygote open by way of a fork called SystemServer, SystemServer open ActivityThread process. Note: Before this operation is done by the system, we can not be optimized (of course, if you have enough cattle, you can re-write a android system optimization).
ActivityThread which has a main (String [] args), the method has the following code

ActivityThread thread=new ActivityThread();
thread.attach(false)

Start their own, in thread.attach (false) method in binds application (in this case, the application creation process and have makeapplication information packages, etc., followed by the application will perform onCreate method). We can optimize from here. This time will do a series of things, and finally create Activity (Activity onCreate method of execution). Our optimization process is to start from the Application initialization, the Activity's onCreate () end.
If you do not do that we may start to optimize a lot of problems, such as black and white issue, we start an app will appear when a white screen. White screen for a few seconds, this is a very good user experience. There are also black screen problem, and is similar to black and white.
Where is the root of our look at the screen in black and white?
When we create a new project when, app there will be a default theme.

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

If no code parent="Theme.AppCompat.Light.DarkActionBar"is black, if this code is black and white. If you phone a good performance, can not see, there are ways that you () method in a delay of three seconds onCreate Application category Thread.sleep(3000);
we find Theme.AppCompat.Light.DarkActionBarthe parent class, has been to find it, will find that he has a father called the Platform.AppCompat.Light class. In the part of the code of the parent class as follows:

 <style name="Platform.AppCompat.Light" parent="android:Theme.Light">
        <item name="android:windowNoTitle">true</item>

        <!-- Window colors -->
        <item name="android:colorForeground">@color/foreground_material_light</item>
        <item name="android:colorForegroundInverse">@color/foreground_material_dark</item>
        <item name="android:colorBackground">@color/background_material_light</item>
        <item name="android:colorBackgroundCacheHint">@color/abc_background_cache_hint_selector_material_light</item>
        <item name="android:disabledAlpha">@dimen/abc_disabled_alpha_material_light</item>
        <item name="android:backgroundDimAmount">0.6</item>
        <item name="android:windowBackground">@color/background_material_light</item>

Finally, we look at a property

<item name="android:windowBackground">@color/background_material_light</item>

Black and white colors is this @ color / background_material_light color. The problem here.
In our first modify, add an attribute in styles

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        //在这里定义自己的颜色,你也可以定义自己的图片
        <item name="android:windowBackground">@color/colorAccent</item>
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

Jingdong is so dry, when activated to load a picture, this picture to be displayed for a few seconds, they onCreate Application category () in Thread.sleep( );a few seconds. Such a played advertising effect
can be changed to a transparent color

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
 //改成透明
         <item name="android:windowIsTranslucent">true</item>
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

There will be a wait into a transparent effect. But even into transparent, it will also consume memory. We look Tencent QQ is how to solve this problem.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:windowBackground">@null</item>
        //禁止预览为真
        <item name="android:windowDisablePreview">true</item>
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

This effect and our transparency effect is the same, there will be a wait for results. But do not consume memory.
But this also has a bad place, willing to be led to each startup activity have emerged wait for the results, we can come out of a single theme, applied to the inlet Activity.

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    //声明启动的style,注意style必须继承AppCompat
    <style name="AppTheme.LauncherStyle">
        <item name="android:windowBackground">@null</item>
        <item name="android:windowDisablePreview">true</item>
    </style>
</resources>

Xml configuration file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.acer.testfanshe">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:theme="@style/AppTheme.LauncherStyle">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

Not yet finished, you need to be set in code

package com.example.acer.testfanshe;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置主题,注意要加下划线
        setTheme(R.style.AppTheme_LauncherStyle);
    }
}

Two: We start optimization can start from three places, one is the Application of onCreate (), one of the Activity onCreate (), is a xml file.

Introduce a performance test method

 @Override
    public void onCreate() {
        super.onCreate();
        Debug.startMethodTracing("文件路径");
        /**
         * do something
         */
        Debug.stopMethodTracing();
    }

It will generate a file that can be opened as the. Check the implementation of those methods for a long time, then optimize. In the onCreate () method was not to do too many things, to open OkHttpClient these things can use lazy loading, download pictures and other network request can open a thread.
Note that open when the thread may report this error java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()in the Application of the onCreate (), if we want to open the thread to save time, you need to pay attention to the following questions
(1) which use the network to request wallets may create Handler, which It is not allowed. In this method can not be created Handler.
(2) can not have UI operations
(3) for asynchronous less demanding; if we open thread execution time is longer, this time the main thread has been executing the Application of the onCreate (); to perform other code, such as Activity's onCreate (); this time, in the onCreate carton Activity () method might report a null pointer exception.
View Activity introduce a startup time-consuming method: Dispalayed keyword
Here Insert Picture Description
Alternatively, you can enter the following command at the command line to view

adb shell am start -W 包名/.xxxActivity

After entering this command starts Activity, cmd is printed out as follows

C:\Users\acer>adb shell am start -W com.example.acer.testfanshe/.MainActivity
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.ex
mple.acer.testfanshe/.MainActivity }
Warning: Activity not started, its current task has been brought to the front
Status: ok
Activity: com.example.acer.testfanshe/.MainActivity
ThisTime: 5791
TotalTime: 5791
WaitTime: 20043
Complete

ThisTime represents the last Activity start time, TotalTime represents the time all Activity start, because we have only one program in MainActivity, all two of them are equal. + Activity waitTime system actuators including start time Here Insert Picture Description
hot start and cold start difference
cold start: app information is loaded into memory
hot start: app information need not be loaded into memory, app information is already in memory. Because there has been a background process. app shell ps command to see whether the process in memory.

Guess you like

Origin blog.csdn.net/qczg_wxg/article/details/89503207