Android boot

Cold startup: The cold startup refers to the startup of the application from the beginning. The system process creates the process after a cold start, calling Appcaliton.onCreate Hot start: In a hot start, all the system does is bring the Activity to the foreground. Appcaliton.oncreate is not called

Statistics of cold startup time

System Log Statistics

You can check the startup time by typing ActivityTaskManager: Displayed in Logcat. The startup time is displayed

Adb command mode does not require root

adb shell am start -S -W [packagename]/[activity]

adb shell am start -S -W com.smzc.driver/.ui.activity.SplashActivity

Cold start

Warm start

ThisTime indicates the start time of the last Activity in a series of starts TotalTime Indicates the time taken to start a new application, including the start of a new process and the start of an Activity, but excluding the time taken for the previous application Activity pasuse

TotalTime is OnWindowFocusChanged to the Activity (Boolean hasForcus)

So how do we know in Appcalition. The onCreate method to the Activity. What method does OnWindowFocusChanged method is time consuming?

You can select this in AndroidStudio or in the constructor of Appcalition

public MyApplication() { @Override public void onCreate() { final File methodTracingFile = new File(getExternalFilesDir("trace"), "AppStart.trace"); / / in the mobile phone of SD Calvin AppStart created a file. The trace / / to have write permission to the Debug file. StartMethodTracing (methodTracingFile. GetPath ()); } public class MainActivity extends AppCompatActivity { @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); Debug.stopMethodTracing(); }Copy the code

Then click the Android Studio Profile to start the Activity. There are some problems with the profile. Huawei and Xiaomi phones will crash and the following effects will not be displayed, so it is not very useful

Flarm Chat

SetContentView takes too long to load the layout, so you can use asynchronous load layout to load the layout in child threads

new AsyncLayoutInflater(this).inflate(R.layout.activity_main, null.new AsyncLayoutInflater.OnInflateFinishedListener() {
              @Override
              public void onInflateFinished(@NonNull View view, int resid, @Nullable ViewGroup parent) { setContentView(view); }});Copy the code

Harsh mode does not determine which methods are time-consuming to operate on the main thread. Enable StrictMode at debug

Optimization of experience in Android performance

We add this theme to the Activity we start to get a black and white screen

So Android :windowBackground can set an image and this is the optimization of the Android startup experience

Start the optimization

  1. Rational use of asynchronous initialization, lazy initialization, lazy loading mechanisms

  2. Avoid time-consuming operations during startup. For example, do not put database I/O operations on the main thread

  3. Class loading optimization: Perform class loading asynchronously ahead of time.

  4. Use idleHandler for lazy initialization

  5. To simplify the layout

IdleHandler is a callback interface to which implementation classes can be added via addIdleHandler in MessageQueue. When the task in MessageQueue is temporarily finished (there are no new tasks or the next task is delayed), the interface is called back to false, it is removed, and the callback continues the next time the message is finished. Returns true for multiple execution false for one execution

Optimization of actual combat experience App

We did this by adding

Debug.startMethodTracing
Copy the code

Method consumes resources when viewing the layout, and then stops it in the OnWindowFocusChanged method of the Activity that you started

Debug.stopMethodTracing()
Copy the code

1. Hierarchical optimization!

View the layout through the LayoutInspector

Debug view layout depth

2. Overrender

Turn on overrender

Caton analysis

The use of Systrace

Systrace systrace.py -t 10 -o d:/mytrace2.html wm gfx input view sched freq -a com.yisingle.simulation.trip.driver

Monitor Caton on the App level

1. Use Looper in UI thread to print log match;

Looper.class public static void loop() { final Printer logging = me.mLogging; if (logging ! = null) { logging.println(">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what); } msg.target.dispatchMessage(msg); if (logging ! = null) { logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); }}Copy the code

The principle of BlockCanaray.

So we can use this log to get the interval to get the execution time of the code

LogMonitor logMoitor=new LogMoitor()

Looper.getMainLooper().setMessageLogging(logMoitor)

Implement the following interface

public interface Printer {
    /** * Write a line of text to the output. There is no need to terminate * the given string with a newline. */
    void println(String x);
}

Copy the code

use

2. Choreographer.FramCallBack;